I have a fee calculator. The port calculates their fees using C#, whereas our backend uses Node. We need to make sure the calculator is perfect; right now, it's out by 1c.
The following C# code:
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine(decimal.Round(1.605m,2));
Console.WriteLine(decimal.Round(1.615m,2));
Console.WriteLine(decimal.Round(1.625m,2));
Console.WriteLine(decimal.Round(1.635m,2));
Console.WriteLine(decimal.Round(1.645m,2));
Console.WriteLine(decimal.Round(1.655m,2));
Console.WriteLine(decimal.Round(1.665m,2));
Console.WriteLine(decimal.Round(1.675m,2));
Console.WriteLine(decimal.Round(1.685m,2));
Console.WriteLine(decimal.Round(1.695m,2));
}
}
Comes out with:
1.60
1.62
1.62
1.64
1.64
1.66
1.66
1.68
1.68
1.68
I need an equivalent rounding function in JS which will give me the same result. I've already tried two:
round = (n, decimals = 2) => {
// return n.toFixed(decimals) // doesn't work
return Number((Math.round(n + 'e' + decimals) + 'e-' + decimals)) // doesn't work
}
console.log(round(1.605))
console.log(round(1.615))
console.log(round(1.625))
console.log(round(1.635))
console.log(round(1.645))
console.log(round(1.655))
console.log(round(1.665))
console.log(round(1.675))
console.log(round(1.685))
console.log(round(1.695))
With toFixed()
the result is:
1.60
1.61
1.63
1.64
1.65
1.66
1.67
1.68
1.69
1.70
With the other one using Math.round
the result is:
1.61
1.62
1.63
1.64
1.65
1.66
1.67
1.68
1.69
1.70
Solutions?