7

I have two negative value -0.245 and -9.085. I want make them to 2 decimal places. I am using JavaScript function toFixed() but getting some weird result.

Please help me to under stand the logic behind the first example being rounded "down" but the second being rounded "up"

//Examples 1. result coming as expected
var num = -0.245
var n = num.toFixed(2); //-0.24
console.log(n);

//Examples 2. result should be -9.08
num = -9.085
n = num.toFixed(2); //-9.09
console.log(n);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33

5 Answers5

5

as per description in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed mention:

The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

For -0.245.toFixed(2), the value is negative and the value after 2 decimal places is -0.045, -0.045 is higher than -0.05, so the result is rounded up to -0.24

For -9.085.toFixed(2), the value is negative and the value after 2 decimal places is -0.085, -0.085 is lower than -0.08, so the result is rounded down to -9.09

Below is solution to always round up to 2 decimal places for negative value.

var num = -9.085
var output = num < 0 ? Math.floor(Math.abs(num) * 100) * -1 / 100 : num.toFixed(2)
console.log(output) //-9.08
alpha
  • 1,103
  • 7
  • 10
  • This may work but doesn't actually answer OP's question - _"Please help me to under stand the logic behind the first example being rounded "down" but the second being rounded "up""_ – Turnip Sep 29 '16 at 11:51
  • @Rajesh since the user want to always round down negative value to 2 decimal places, we can convert the value to positive number and use Math.floor to round down value to 2 decimal places – alpha Sep 29 '16 at 11:53
  • 1
    Try `0.245` or `0.236`. Both returns `0.24` – Rajesh Sep 29 '16 at 12:15
  • correction to my previous comment, it should be: since the OP want to always round up negative value to 2 decimal places, we can convert the value to positive number and use Math.floor to round value to nearest integer(round down in this case for positive number) then convert back to negative number – alpha Sep 29 '16 at 12:42
1

toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

Which means the result is not wrong but you expecting toFixed() to do something diffrent. If you round -9.085 to second decimal place the mathematic correct result is -9.09 (.5 round up the next decimal). What you are trying to accomplish is to remove everything after the second decimal which can be accomplished with:

parseInt(-9.085 * 100) / 100 //-9.08

alert(parseInt(-9.085 * 100) / 100)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Code Spirit
  • 3,992
  • 4
  • 23
  • 34
  • How is that logical. Rounding -9.085 UP is -9.08 - your partInt does work better than a parseFloat does. – mplungjan Sep 29 '16 at 11:17
  • Im sory i forgot that its a negative number. But I looked it up and there is no right rule how to round but multiple. It seems JavaScript decides to round it like a positive integer. In wikipedia it says, negative numbers are rounded by absolute values. – Code Spirit Sep 29 '16 at 11:23
  • Also your code may fail on some numbers that don't align well – mplungjan Sep 29 '16 at 11:26
  • How does it fail? It rounds the number to the second decimal no matter how large it is. – Code Spirit Sep 29 '16 at 11:30
  • In this case it seems to work, but not all clean divisions work in JS – mplungjan Sep 29 '16 at 11:42
0

Please check, this helps you alot in understanding the logic behind this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Ajay Thakur
  • 1,066
  • 7
  • 23
  • 2
    Please do not just post a link. I already looked at that site and it does not explain the issue – mplungjan Sep 29 '16 at 11:13
  • @mplungjan refer http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.4.5, point `8.a`. There is some issue in parsing `0. ...` values. – Rajesh Sep 29 '16 at 11:36
  • In this function, toFixed() roundoff is working when your value before the roundoff value is greater than or equal to five. For Example: In this case (0.245).toFixed(2); there is 4 which is less than roundoff value so it does'nt increase 4 to 5. But in this case (0.255).toFixed(2); it show 0.26. – Ajay Thakur Sep 29 '16 at 11:37
0

This function rounds off the values. E.g. if the value is between 9.080 to 9.084 the result would be 9.08 and if the value is 9.085 to 9.089 the result would be 9.09.

kk.
  • 3,747
  • 12
  • 36
  • 67
  • That is the logic for POSTIVE numbers. It would be logical that -9.085 would be rounded to -9.08 – mplungjan Sep 29 '16 at 11:14
  • 2
    This also doesn't explain why the two examples are rounded differently. – Turnip Sep 29 '16 at 11:15
  • @mplungjan: I did not get your point of rounding off would be different for +ve and -ve numbers. Please elaborate. – kk. Sep 29 '16 at 11:18
  • Show me why it should be the same? Rounding UP is adding something to the number in my opinion. I may be wrong since I never needed to round negative numbers – mplungjan Sep 29 '16 at 11:22
  • If you are not sure then was there any point in downvoting my answer @mplungjan? – kk. Sep 29 '16 at 14:45
-1

enter image description here

  parseFloat(Math.round(num * 100) / 100).toFixed(2);
Mirza Obaid
  • 1,707
  • 3
  • 23
  • 35
  • Make a snippet. Then use 9.085 – mplungjan Sep 29 '16 at 11:20
  • This answer doesn't really describe how it solves the problem. Consider editing it to include some details? Also not a good idea to include a screenshot of code; instead write out the code using SO's markdown. – brandonscript Sep 29 '16 at 18:52