Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply C# equivalent rounding method in Javascript

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.

like image 371
Naveen Kumar V Avatar asked Sep 28 '17 13:09

Naveen Kumar V


2 Answers

You 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
like image 126
Samuel Neff Avatar answered Sep 22 '22 16:09

Samuel Neff


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
like image 43
Shahar Shokrani Avatar answered Sep 24 '22 16:09

Shahar Shokrani