5

I need to round off the decimal value to decimal places using javascript.

Ex,:

16.181 to 16.18
16.184 to 16.18
16.185 to 16.19
16.187 to 16.19

I have found some answers, but most of them do not round off 16.185 to 16.19..

JohnFx
  • 34,542
  • 18
  • 104
  • 162
Prasad
  • 58,881
  • 64
  • 151
  • 199
  • @mplungjan, I am not asking the difference between "Math.round(0) and toFixed(0)". If you read my question, you can understand that i am having the problem in rounding 16.185 to 16.19. – Prasad Apr 04 '11 at 07:12
  • I am actually surprised to see that the JavaScript says `16.185 * 100` = `1618.4999999999998` in FF/Chrome and `1618.5` in IE. Very strange. – Salman A Apr 04 '11 at 07:12
  • You are correct. My mistake - must however be a duplicate of something ;) – mplungjan Apr 04 '11 at 08:21
  • @mplungjan, the post you pointed needs 1.5555 to 1.55, but the output i need is like 1.5555 to 1.56. Anyway @ayk answered it. – Prasad Apr 04 '11 at 08:48
  • Yes. This one os way better: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – mplungjan Apr 04 '11 at 09:28

8 Answers8

13
(Math.round((16.185*Math.pow(10,2)).toFixed(1))/Math.pow(10,2)).toFixed(2);

If your value is, for example 16.199 normal round will return 16.2... but with this method youll get last 0 too, so you see 16.20! But keep in mind that the value will returned as string. If you want to use it for further operations, you have to parsefloat it :)

And now as function:

function trueRound(value, digits){
    return (Math.round((value*Math.pow(10,digits)).toFixed(digits-1))/Math.pow(10,digits)).toFixed(digits);
}
ayk
  • 1,330
  • 2
  • 15
  • 24
  • this is useful. If you change it to look like function, it may be useful for someone who has the same problem as mine. function formatNumber(myNum, numOfDec) { return (Math.round((myNum * Math.pow(10, numOfDec)).toFixed(1)) / Math.pow(10, numOfDec)).toFixed(numOfDec); } – Prasad Apr 04 '11 at 07:30
  • This is nice but still imprecise, `roundTo(89.68449, 2)` should be `89.69`, but is `89.68`. – mrts Jan 01 '18 at 11:11
2

Thanks @ayk for your answer, I modified your function into this :

function trueRound(value, digits){
    return ((Math.round((value*Math.pow(10,digits)).toFixed(digits-1))/Math.pow(10,digits)).toFixed(digits)) * 1;
}

just add " *1 " because with yours, as you wrote, 16.2 becomes 16.20 and I don't need the zero in the back.

gdoron
  • 147,333
  • 58
  • 291
  • 367
eve
  • 21
  • 1
1

Use-

Decimal ds=new Decimal(##.##);
String value=ds.format(inputnumber);

This will work perfectly in my case ,hope it will work 100%

0

[EDIT] This code does not work for a value like 18.185, as 18.185 * Math.pow(10,2) is being evaluated to 1818.4999999999997

Math.round(<value> * Math.pow(10,<no_of_decimal_places>)) / Math.pow(10,<no_of_decimal_places>) ;

Example: 1234.5678 --> 2 decimal places --> 1234.57

Math.round(1234.5678 * Math.pow(10,2)) / Math.pow(10,2) ; 
das_weezul
  • 6,082
  • 2
  • 28
  • 33
  • I wrote 10^ instead of Math.pow(10,) --> Fixed it – das_weezul Apr 04 '11 at 07:04
  • This is also not working. It rounds off 16.185 as 16.18. I need to round it as 16.19 – Prasad Apr 04 '11 at 07:05
  • This is correct solution but surprisingly, FF and Chrome seem to have strange rounding off problems. – Salman A Apr 04 '11 at 07:16
  • Problem is that the result of rounding the value multiplyed with 10^2 will result at .5 values as .4999999999998 and it will be rounded down... – ayk Apr 04 '11 at 07:18
  • @Salman, I have tried this in IE, Firefox and Chrome, all returns 16.18 on rounding off 16.185 – Prasad Apr 04 '11 at 07:20
  • Yes, this beats me too. Even a simple test such as `Math.round(16.185*100) + ' ' + Math.round(1618.5)`... returns `1618 1619`... totally inconsistent. – Salman A Apr 04 '11 at 07:31
0
function formatNumber(myNum, numOfDec) {
        var dec = Math.pow(10, numOfDec);
        return Math.round(myNum * dec + 0.1) / dec;
}
Dmitry Evseev
  • 11,533
  • 3
  • 34
  • 48
0

The code you are using for rounding off is correct. But to get the desired result please remove the .toFixed(numOfDec) from the code.

The function:

function formatNumber(myNum, numOfDec) {

    var decimal = 1
    for (i = 1; i <= numOfDec; i++)
        decimal = decimal * 10 //The value of decimal determines the number of decimals to be rounded off with (.5) up rule 
    var myFormattedNum = Math.round(myNum * decimal) / decimal
    return (myFormattedNum)
}

Hope it helps you in some way :)

Wasim
  • 896
  • 7
  • 24
0
parseFloat(myNum.toFixed(numOfDec))
vvk
  • 833
  • 5
  • 16
0

You COULD try a simpler function...

function roundOffTo(number, place) {
    return Math.round(number * place) / place;
}

How does it work? The place can be 10, 100, 1000, 10000, and so on. The reverse will be 0.1, 0.01, 0.001, and so on. Let's say your number is 16.185, and your place is 100. The first thing it'll do is to multiply the number by the place, which is 1618.5. Rounding it off by Math.round will result in 1619. Dividing by the place gives us 16.19. There. By the way, no need to worry about the .5 problem today, it's kinda fixed. But just in case it isn't, change Math.round(number * place) to Math.round(number * place + 0.1).

Sci Pred
  • 33
  • 4