Looking for help in rounding the money to the nearest 0.05 in js language.
Input & Expected Output:
JavaScript
x
11
11
1
1.10 => 1.10
2
1.11 => 1.10 (round down)
3
1.12 => 1.10 (round down)
4
1.13 => 1.15 (round up)
5
1.14 => 1.15 (round up)
6
1.15 => 1.15
7
1.16 => 1.15 (round down)
8
1.17 => 1.15 (round down)
9
1.18 => 1.20 (round up)
10
1.19 => 1.20 (round up)
11
Advertisement
Answer
You could take an offset and take a multiple floored value.
If you need zeroes at the end take .toFixed(2)
.
JavaScript
1
3
1
const format = f => Math.floor((f + 0.025) * 20) / 20;
2
3
console.log([1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19].map(format));
JavaScript
1
1
1
.as-console-wrapper { max-height: 100% !important; top: 0; }