Is there a way in javascript or Typescript to format decimal?
For example:
1.33 => 1.35
1.32 => 1.35
All i found was formatting to 2 decimals, but i need it to format to nearest .05
Is there a way in javascript or Typescript to format decimal?
For example:
1.33 => 1.35
1.32 => 1.35
All i found was formatting to 2 decimals, but i need it to format to nearest .05
This function uses Math.round
and multiplies and divides by 0.05 because it is to the nearest 0.05.
function tN(n) {
return Math.round(n / 0.05) * 0.05;
}
console.log(tN(1.33));
console.log(tN(1.32));
console.log(tN(0.05));
console.log(tN(-1.08));
If all outputs have to end in a 5:
function tN(n) {
return n % 0.1 > 0.05 ? Math.floor(n / 0.05) * 0.05 : Math.ceil(n / 0.05) * 0.05;
}
console.log(tN(1.33));
console.log(tN(1.32));
console.log(tN(0.05));
console.log(tN(-1.08));