6

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..

5
  • @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. Commented Apr 4, 2011 at 7: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. Commented Apr 4, 2011 at 7:12
  • You are correct. My mistake - must however be a duplicate of something ;) Commented Apr 4, 2011 at 8: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. Commented Apr 4, 2011 at 8:48
  • Yes. This one os way better: stackoverflow.com/questions/149055/… Commented Apr 4, 2011 at 9:28

8 Answers 8

14
(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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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); }
This is nice but still imprecise, roundTo(89.68449, 2) should be 89.69, but is 89.68.
3

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.

Comments

1

Use-

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

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

Comments

1

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).

Comments

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 :)

2 Comments

i already tried that and got the same problem. Its not rounding 16.185 to 16.19
@ Prasad try my solution it will result in what you need!
0
function formatNumber(myNum, numOfDec) {
        var dec = Math.pow(10, numOfDec);
        return Math.round(myNum * dec + 0.1) / dec;
}

3 Comments

This is not working. It rounds off 16.185 as 16.18. I need to round it as 16.19.
Hm.. I see) this is the issue
You know, you can go this way, add extra 0.1 to your value, to prevent cases with 5 value: return Math.round(myNum * dec + 0.1) / dec;
0
parseFloat(myNum.toFixed(numOfDec))

Comments

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) ; 

6 Comments

I wrote 10^<no_of_decimal_places> instead of Math.pow(10,<no_of_decimal_places>) --> Fixed it
This is also not working. It rounds off 16.185 as 16.18. I need to round it as 16.19
This is correct solution but surprisingly, FF and Chrome seem to have strange rounding off problems.
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...
@Salman, I have tried this in IE, Firefox and Chrome, all returns 16.18 on rounding off 16.185
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.