119

in JavaScript, the typical way to round a number to N decimal places is something like:

function roundNumber(num, dec) {
  return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}

function roundNumber(num, dec) {
  return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}

console.log(roundNumber(0.1 + 0.2, 2));
console.log(roundNumber(2.1234, 2));

However this approach will round to a maximum of N decimal places while I want to always round to N decimal places. For example "2.0" would be rounded to "2".

Any ideas?

Dan Dascalescu
  • 127,343
  • 44
  • 300
  • 387
hoju
  • 26,725
  • 37
  • 129
  • 173
  • 10
    normally, you could use `toFixed()` ( https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Number/ToFixed ), but it's buggy in IE: http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript/661757#661757 ; you'll have to write your own version... – Christoph Feb 08 '10 at 11:26
  • 1
    @hoju - perhaps change accepted answer - David's answer is correct for IE8+, while the accepted answer has some serious bugs on all browsers. – robocat May 26 '15 at 23:02
  • @robocat: Are you serious? – Guffa May 27 '15 at 00:51

9 Answers9

267

I think that there is a more simple approach to all given here, and is the method Number.toFixed() already implemented in JavaScript.

simply write:

var myNumber = 2;

myNumber.toFixed(2); //returns "2.00"
myNumber.toFixed(1); //returns "2.0"

etc...

ᅙᄉᅙ
  • 17,132
  • 10
  • 64
  • 97
David
  • 3,931
  • 2
  • 22
  • 29
  • Where is mentioned, hoju? I reviewed other answers and I didn't find anyone reporting that toFixed function is buggy. Thanks – David Apr 21 '14 at 07:40
  • @David: That's mentioned in a comment to the answer, for example. – Guffa Jun 05 '15 at 15:55
  • 23
    caution: `toFixed()` returns a string. – Jordan Arseno Dec 01 '15 at 22:33
  • 2
    @JordanArseno this can be fixed using parseInt(myNumber.toFixed(intVar)); to return an integer value, or parseFloat(myNumber.toFixed(floatVar)); to return a float if user has decimal places. – Stephen Romero Aug 03 '18 at 20:24
52

I found a way. This is Christoph's code with a fix:

function toFixed(value, precision) {
    var precision = precision || 0,
        power = Math.pow(10, precision),
        absValue = Math.abs(Math.round(value * power)),
        result = (value < 0 ? '-' : '') + String(Math.floor(absValue / power));

    if (precision > 0) {
        var fraction = String(absValue % power),
            padding = new Array(Math.max(precision - fraction.length, 0) + 1).join('0');
        result += '.' + padding + fraction;
    }
    return result;
}

Read the details of repeating a character using an array constructor here if you are curious as to why I added the "+ 1".

Community
  • 1
  • 1
Elias Zamaria
  • 89,268
  • 31
  • 107
  • 141
  • Passing in a value of 708.3333333333333 with a precision of 2 or 3 results in a return value of 708.00 for me. I need this for 2 decimal places, in Chrome and IE 9 .toFixed(2) met my needs. – alan Sep 20 '13 at 14:48
  • this is not a common way, e.g, toFixed(16.775, 2) return 16.77. Convert number to String then convert is the only way. – hiway Jun 04 '14 at 03:49
  • 2
    There is a bug with this method: toFixed(-0.1111, 2) returns 0.11, i.e. the negative sign is lost. – Matt Oct 20 '14 at 02:40
  • Works great, except for that I have added parseFloat(result) at the end. Worth an edit. – Artur Barseghyan Mar 25 '15 at 17:01
26

That's not a rounding ploblem, that is a display problem. A number doesn't contain information about significant digits; the value 2 is the same as 2.0000000000000. It's when you turn the rounded value into a string that you have make it display a certain number of digits.

You could just add zeroes after the number, something like:

var s = number.toString();
if (s.indexOf('.') == -1) s += '.';
while (s.length < s.indexOf('.') + 4) s += '0';

(Note that this assumes that the regional settings of the client uses period as decimal separator, the code needs some more work to function for other settings.)

Guffa
  • 666,277
  • 106
  • 705
  • 986
  • 3
    toFixed is buggy..for instance the decimal: 1.02449999998 if u do dec.toFixed(4) => 1.0244. It should have been 1.0245 instead. – Bat_Programmer Sep 19 '11 at 01:58
  • 2
    @deepeshk But what would be the problem with using `toFixed()` to pad decimals at the end, after rounding? – pauloya Dec 15 '11 at 17:11
  • 10
    @deepeshk in what browser? Just tried it in Chrome 17 and `1.02449999998.toFixed(4)` correctly returns `1.0245`. – Matt Ball Mar 13 '12 at 14:40
  • @Mark got most of the answer, but after some research you need to call it like this, `Number(float).toFxied(places);`. – Mark Tomlin May 21 '13 at 00:28
  • 3
    @MarkTomlin: Then it seems that you have a string instead of a number. – Guffa May 21 '13 at 01:14
  • @Guffa thanks' for the clarification, I'm kinda fumbling my way through JavaScript programming right now. +1. – Mark Tomlin May 21 '13 at 03:32
  • As an FYI, if you only need precision to 2 decimal places .toFixed works for both IE and Chrome. – alan Sep 20 '13 at 14:50
  • 1
    .toFixed() works brilliantly in modern browsers (I tested Chrome, Firefox, IE11, IE8). @Bat_Programmer - please clarify *which* browsers you think have that bug. – robocat May 07 '15 at 03:57
  • 1
    @robocat: http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript/661757#661757 – Guffa May 07 '15 at 07:58
  • 1
    `.toFixed()` is fixed in IE8+ (my testing) but apparently buggy in IE7 (as per @Guffa link in comment above - thank you). So **use** toFixed() unless you need to support IE7 ;-) – robocat May 26 '15 at 22:45
  • 1
    Do NOT USE this answer because it has serious bugs: it gives incorrect answers: NaN -> NaN.000, Infinity -> Infinity.000, 9e60 -> 9E60.000, 0.000001 -> 0.000001 (0.000001.toFixed(3) correctly gives 0.000). – robocat May 26 '15 at 23:01
  • 1
    @robocat: Are you serious? – Guffa May 27 '15 at 00:50
  • 1
    @Guffa - `.toFixed()` works *correctly* in IE8+. e.g. I just run IE8 using the example from the link of `(0.595).toFixed(2)` and got correct answer of `0.60`. Your code is broken for corner cases (the few I thought of from the top of my head with a few minutes testing, I could probably find other things wrong with it), and is not suitable for production code IMHO. – robocat May 28 '15 at 03:40
  • 1
    @robocat: You are looking for something that I never claimed that the code would be. Besdies, you can just as easily find corner cases where `toFixed` is broken, for example `(1000000000000000000000).toFixed(3)` returns `1e+21` not `1000000000000000000000.000`. – Guffa May 28 '15 at 08:29
19

There's always a better way for doing things. Use toPrecision -

var number = 51.93999999999761;

I would like to get four digits precision: 51.94

just do:

number.toPrecision(4);

the result will be: 51.94

Brian Burns
  • 17,878
  • 8
  • 77
  • 67
de.la.ru
  • 2,685
  • 1
  • 25
  • 32
6

PHP-Like rounding Method

The code below can be used to add your own version of Math.round to your own namespace which takes a precision parameter. Unlike Decimal rounding in the example above, this performs no conversion to and from strings, and the precision parameter works same way as PHP and Excel whereby a positive 1 would round to 1 decimal place and -1 would round to the tens.

var myNamespace = {};
myNamespace.round = function(number, precision) {
    var factor = Math.pow(10, precision);
    var tempNumber = number * factor;
    var roundedTempNumber = Math.round(tempNumber);
    return roundedTempNumber / factor;
};

myNamespace.round(1234.5678, 1); // 1234.6
myNamespace.round(1234.5678, -1); // 1230

from Mozilla Developer reference for Math.round()

jaggedsoft
  • 3,537
  • 1
  • 31
  • 40
JohnnyBizzle
  • 963
  • 1
  • 16
  • 30
6

This works for rounding to N digits (if you just want to truncate to N digits remove the Math.round call and use the Math.trunc one):

function roundN(value, digits) {
   var tenToN = 10 ** digits;
   return /*Math.trunc*/(Math.round(value * tenToN)) / tenToN;
}

Had to resort to such logic at Java in the past when I was authoring data manipulation E-Slate components. That is since I had found out that adding 0.1 many times to 0 you'd end up with some unexpectedly long decimal part (this is due to floating point arithmetics).

A user comment at Format number to always show 2 decimal places calls this technique scaling.

Some mention there are cases that don't round as expected and at http://www.jacklmoore.com/notes/rounding-in-javascript/ this is suggested instead:

function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
George Birbilis
  • 2,639
  • 2
  • 30
  • 33
  • btw, POSITS are promising for future h/w architectures: https://www.nextplatform.com/2019/07/08/new-approach-could-sink-floating-point-computation/ – George Birbilis Aug 08 '19 at 13:12
2

Hopefully working code (didn't do much testing):

function toFixed(value, precision) {
    var precision = precision || 0,
        neg = value < 0,
        power = Math.pow(10, precision),
        value = Math.round(value * power),
        integral = String((neg ? Math.ceil : Math.floor)(value / power)),
        fraction = String((neg ? -value : value) % power),
        padding = new Array(Math.max(precision - fraction.length, 0) + 1).join('0');

    return precision ? integral + '.' +  padding + fraction : integral;
}
Christoph
  • 158,247
  • 36
  • 176
  • 234
  • Your code has a bug. I tried toFixed(2.01, 4) and got a result of "2.100". If I ever find a way to fix it, I will post it as an answer to this question. – Elias Zamaria May 25 '10 at 23:28
  • @mikez302: the padding computation was off by one; should work now, but feel free to bug me again if it's still broken... – Christoph May 27 '10 at 14:32
  • Very strange. When I run this fuction with the firebug console open in firefox 17 it freezes the whole browser like js is caught in an endless loop. Even if i do not console.log the output. If I do not have firebug activated the bug does not occur. – Rick Kukiela Jan 07 '13 at 10:40
  • 1
    Update, i ran it in chrome and i get: Uncaught RangeError: Maximum call stack size exceeded in regards to the toFixed function call. – Rick Kukiela Jan 07 '13 at 10:42
  • @SublymeRick: I have no idea why this happens; shot in the dark: try renaming the function... – Christoph Jan 07 '13 at 11:01
  • @Christoph, I believe it is happening because of stacking all vars and calculations with comma on a single var statement. It breaks the debugger, but works in browser.I had similar case with a larger script, if I remember correctly on Firefox 42 – AaA Nov 06 '17 at 02:15
2

I think below function can help

function roundOff(value,round) {
   return (parseInt(value * (10 ** (round + 1))) - parseInt(value * (10 ** round)) * 10) > 4 ? (((parseFloat(parseInt((value + parseFloat(1 / (10 ** round))) * (10 ** round))))) / (10 ** round)) : (parseFloat(parseInt(value * (10 ** round))) / ( 10 ** round));
}

usage : roundOff(600.23458,2); will return 600.23

KRISHNA TEJA
  • 141
  • 1
  • 13
-2

If you do not really care about rounding, just added a toFixed(x) and then removing trailing 0es and the dot if necessary. It is not a fast solution.

function format(value, decimals) {
    if (value) {
        value = value.toFixed(decimals);            
    } else {
        value = "0";
    }
    if (value.indexOf(".") < 0) { value += "."; }
    var dotIdx = value.indexOf(".");
    while (value.length - dotIdx <= decimals) { value += "0"; } // add 0's

    return value;
}
Juan Carrey
  • 676
  • 1
  • 6
  • 13