I am trying to build a function that can return correct rounding off for both positive and negative values.
Here is my requirement
Input Output
-1.8550 -1.86
1.8550 1.86
-1384.8540 -1384.85
1384.8540 1384.85
-1384.8550 -1384.86
-1384.8560 -1384.86
-3203.8640 -3203.86
I tried below, but its not working.!
var customRoundOff = function(value) {
debugger;
var valueSign = Math.sign(value);
var numericalPart = Math.abs(Number((value + "").split(".")[0]));
var decPart = (value + "").split(".")[1];
if (decPart && numericalPart) {
var pntedDecimal = Number('0.' + decPart);
var roundedDecimal = pntedDecimal.toFixed(2);
var roundedValue = numericalPart + roundedDecimal;
console.log("pntedDecimal", pntedDecimal);
console.log("roundedDecimal", roundedDecimal);
console.log("numericalPart", numericalPart);
console.log("roundedValue", roundedValue);
return roundedValue * valueSign; // Returns float
} else {
return value.toFixed(2); // Returns string
}
}
customRoundOff(-1.8550);