I am implementing an Hybrid mobile application in which I have to represent our Desktop application written in C#.
When rounding off a value, the value differs between Desktop and Mobile application.
Example
Code used in C#:
Math.Round (7060.625, 2); // prints 7060.62
Math.Round (7060.624, 2); // prints 7060.62
Math.Round (7060.626, 2); // prints 7060.63
Code used in JS:
+(7060.625).toFixed(2); // prints 7060.63 (value differs)
+(7060.624).toFixed(2); // prints 7060.62
+(7060.626).toFixed(2); // prints 7060.63
How can I change the JS code to represent the value as in C#.
Note:
We can make C# to represent the value as in JS, using Math.Round (7060.625, 2, MidpointRounding.AwayFromZero);
But I have no option to change in there.
EDIT 1
Rounding off decimals is not fixed. It is chosen by user in mobile application.
I am implementing an Hybrid mobile application in which I have to represent our Desktop application written in C#.
When rounding off a value, the value differs between Desktop and Mobile application.
Example
Code used in C#:
Math.Round (7060.625, 2); // prints 7060.62
Math.Round (7060.624, 2); // prints 7060.62
Math.Round (7060.626, 2); // prints 7060.63
Code used in JS:
+(7060.625).toFixed(2); // prints 7060.63 (value differs)
+(7060.624).toFixed(2); // prints 7060.62
+(7060.626).toFixed(2); // prints 7060.63
How can I change the JS code to represent the value as in C#.
Note:
We can make C# to represent the value as in JS, using Math.Round (7060.625, 2, MidpointRounding.AwayFromZero);
But I have no option to change in there.
EDIT 1
Rounding off decimals is not fixed. It is chosen by user in mobile application.
Share Improve this question asked Sep 28, 2017 at 13:43 Naveen Kumar VNaveen Kumar V 2,8092 gold badges31 silver badges47 bronze badges 10- 2 I don't think the linked question is the same. Sure, its about rounding, but this question isn't just about general rounding. Its trying to get it to round down a decimal that ends in 5 but still round up for 6-9. (duplicate has been removed) – Blake Thingstad Commented Sep 28, 2017 at 13:49
- 1 Difference is that in case of 5 JS rounds UP and C# down. Isn't the round up in case of 5 desired. Math suggests so. math.stackexchange./questions/218299/… – CodeSmith Commented Sep 28, 2017 at 13:54
- 2 @DanteTheSmith it's not that simple as up vs down. C# by default rounds toward the even, so 1.5 rounds to 2 and 2.5 also rounds to 2. C# has two rounding modes.. toward even and away from zero. Java by contrast has seven or eight, depending on what you count as an actual rounding mode. docs.oracle./javase/7/docs/api/java/math/RoundingMode.html – Samuel Neff Commented Sep 28, 2017 at 13:56
- 1 @Igor not the same issue.. that question you referenced as a duplicate is about dealing with floating point issues and the question here is about an alternate rounding algorithm that JS doesn't support natively. – Samuel Neff Commented Sep 28, 2017 at 13:57
- 1 @SamuelNeff oh god I didn't realize rounding had so many confusing options! Rounding towards even, towards 0, positive infinity, down, up, etc, too much for me! – Blake Thingstad Commented Sep 28, 2017 at 13:58
2 Answers
Reset to default 16You need a custom implementation of rounding to implement "banker's rounding" or to-even rounding.
From:
Gaussian/banker's rounding in JavaScript
function evenRound(num, decimalPlaces) {
var d = decimalPlaces || 0;
var m = Math.pow(10, d);
var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
var i = Math.floor(n), f = n - i;
var e = 1e-8; // Allow for rounding errors in f
var r = (f > 0.5 - e && f < 0.5 + e) ?
((i % 2 == 0) ? i : i + 1) : Math.round(n);
return d ? r / m : r;
}
console.log( evenRound(1.5) ); // 2
console.log( evenRound(2.5) ); // 2
console.log( evenRound(1.535, 2) ); // 1.54
console.log( evenRound(1.525, 2) ); // 1.52
If you control both client side and server side you can follow this super simple pattern, it works fine for edge cases and regular case:
Let's look at 2.155, 2.145 (Midpoint issue) and 2.166, 2.146 (Regular).
C#:
public static decimal RoundFromJavaScript(this Decimal value)
{
return Decimal.Round(value, 2, MidpointRounding.AwayFromZero);
}
//-Midpoint Issue
RoundFromJavaScript(2.155); --> 2.16
RoundFromJavaScript(2.145); --> 2.15
//-Regular
RoundFromJavaScript(2.166); --> 2.17
RoundFromJavaScript(2.146); --> 2.15
*I've omitted the m decimal sign, it should be 2.155m
JavaScript:
function roundToTwo(num) {
return +(Math.round(num + "e+2") + "e-2");
}
//-Midpoint Issue
roundToTwo(2.155) --> 2.16
roundToTwo(2.145) --> 2.15
//-Regular
roundToTwo(2.166) --> 2.17
roundToTwo(2.146) --> 2.15