What is the syntax for the toFixed() method?
Is it possible to use toFixed() on non-numeric values?
What if I pass a negative argument to toFixed()?
How about this?
function wacky_round(number, places) {
var multiplier = Math.pow(10, places+2); // get two extra digits
var fixed = Math.floor(number*multiplier); // convert to integer
fixed += 44; // round down on anything less than x.xxx56
fixed = Math.floor(fixed/100); // chop off last 2 digits
return fixed/Math.pow(10, places);
}
1.5554 => 1.55
1.5555 => 1.55
1.5556 => 1.56
1.5651 => 1.56
So, that works, but I think you'll find that it's not a generally-accepted way to round. http://en.wikipedia.org/wiki/Rounding#Tie-breaking
And standard function
fixedTo = function (number, n) {
var k = Math.pow(10, n);
return (Math.round(number * k) / k);
}
and then call
fixedTo(1.5555, 2) // 1.56
fixedTo(1.5555, 2) // 1.556
fixedTo(0.615, 2) // 0.62
It doesn't work insanely. You tell the function to keep two decimal digits and that's what it does. If you want to round your number, you can try multiplying your number with 10^x, using Math.round and then dividing by the same number:
const roundToDecimals = function (number, decimals) {
const num = Math.pow(10, decimals);
return Math.round(number * num) / num;
}
console.log(roundToDecimals(0.175, 2));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You have told to round it till 2 points .so the answer will be 0.17 because the third number is less than or equal to 5.
As stated in the documentation, toFixed() does round when necessary. The rounding behavior is to round in the range -.5 < x <= +.5 of the digit.
The strange behavior you're observing is consistent with the note in the documentation linked above:
Floating point numbers cannot represent all decimals precisely in binary which can lead to unexpected results such as 0.1 + 0.2 === 0.3 returning false.
In other words, this is a classic case of floating point precision loss, a problem you'll encounter in virtually any language. If you observe the full outputs of a and b, you'll see that a == 0.075 and b == 0.07500000000000001 due to floating point precision, and thus given these values, it is consistent with the defined rounding behavior to round a to .07 and b to .08.
The problem you are encountering is not specific to JavaScript. It is common to computing in general.
Both these arithmetic calculations have the same result, 0.075:
- 0.25 * 0.3 = 0.075
- 0.025 * 3 = 0.075
This is using the decimal number system commonly used.
Computers, at their core, however, don't use the decimal system, but binary. Everything is based on 0 and 1.
Because of this, they actually have a hard time getting the calculation above right. JavaScript and other programming languages have to approximate the result, giving you this:
- 0.25 * 0.3 = 0.75
- 0.025 * 3 = 0.07500000000000001
You can now see why toFixed returns different results:
- 0.75.toFixed(2) = 0.07
- 0.07500000000000001.toFixed(2) = 0.08
Taken from MDN:
Syntax
numObj.toLocaleString([locales [, options]])
toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:
minimumFractionDigits
The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
To be able to set the options without setting the locale, you can pass undefined as first argument:
var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00
However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)
var num = 2046430.123;
console.log(num.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
})); // 2,046,430.12
I came here because I would like to show the currency symbol (R$) in addition show two digits after the decimal point in the result.
Initially I was trying the following code:
`Amount ${(gas * litros).toFixed(2)
.toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'})}`
Expected output: Total a pagar R$ 20.95
Output: Total a pagar 20.95
So, with the answers above, I tried without the toFixed():
`Total a pagar ${(gas * litros)
.toLocaleString('pt-BR', {style: 'currency', currency: 'BRL', minimumFractionDigits: 2})}`
Output: Total a pagar R$ 15,80