Reputation: 7032
Here's the scenario: I'm getting .9999999999999999
when I should be getting 1.0
.
I can afford to lose a decimal place of precision, so I'm using .toFixed(15)
, which kind of works.
The rounding works, but the problem is that I'm given 1.000000000000000
.
Is there a way to round to a number of decimal places, but strip extra whitespace?
Note: .toPrecision
isn't what I want; I only want to specify how many numbers after the decimal point.
Note 2: I can't just use .toPrecision(1)
because I need to keep the high precision for numbers that actually have data after the decimal point. Ideally, there would be exactly as many decimal places as necessary (up to 15).
Upvotes: 112
Views: 139486
Reputation: 150
I have tried almost all methods suggested by experts here and there.
Finally, I am satisfied with my own answer. As I came to know Math.round is more precise than .toFixed() method, I have used Math.round method.
Math.round(parseFloat(numberString)*100000)/100000; // to round upto 5 decimal places
Math.round(parseFloat(numberString)*1000)/1000; // to round upto 3 decimal places
For Example
Math.round(parseFloat("1.234567")*100000)/100000 >>> 1.23457
Math.round(parseFloat("1.234567")*1000)/1000 >>> 1.235
Math.round(parseFloat("1.001")*100000)/100000 >>> 1.001
Math.round(parseFloat("1.001")*1000)/1000 >>> 1.001
Math.round(parseFloat("1.0010005")*100000)/100000 >>> 1.001
Math.round(parseFloat("1.0010005")*1000)/1000 >>> 1.001
Math.round(parseFloat("1.001000")*100000)/100000 >>> 1.001
Math.round(parseFloat("1.001000")*1000)/1000 >>> 1.001
Math.round(parseFloat("0.000100")*100000)/100000 >>> 1.0E-4
Math.round(parseFloat("0.000100")*1000)/1000 >>> 0.0
Upvotes: 1
Reputation: 81
You can use The toFixed() method to format a number using fixed-point notation. Keep in mind that the parameter may be a value between 0 and 20. This returns your rounded number as a string, but it will still contain the trailing zeros. You can then use parseFloat to get your rounded number without the trailing zeros. Examples:
function prettyRound(num, decimals) {
return parseFloat(num.toFixed(decimals));
}
const numberToRound = 1.12006
console.log(prettyRound(numberToRound, 3))
const anotherToRound = 1.10006
console.log(prettyRound(anotherToRound, 4))
Upvotes: 1
Reputation: 79
most efficient and bets method I found is below
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
Upvotes: 2
Reputation: 24710
The toFixed()
method formats a number
using fixed-point notation, and returns a string
.
It applies a half-up rounding strategy.
(0.124).toFixed(2); // returns 0.12
(0.125).toFixed(2); // returns 0.13
As you described, it will indeed also result in (potentially unnecessary) trailing zeroes sometimes.
(0.001).toFixed(2); // returns 0.00
You may not want to get rid of those trailing zeroes, essentially you could just convert it back to a number
. There are many ways to do this.
+(0.001).toFixed(2); // the shortest
For an overview, of the different methods to convert strings to numbers, please check this question, which has some excellent answers.
Upvotes: 1
Reputation: 1003
If you cast the return value to a number, those trailing zeroes will be dropped. This is also less verbose than parseFloat()
is.
+(4.55555).toFixed(2);
//-> 4.56
+(4).toFixed(2);
//-> 4
This uses the unary + operator, so if using this as part of a string operation you need to have an infix + before it: var n=0.9999999999999999; console.log('output ' + +n.toFixed(2));
. FYI a unary + in front of a string converts it to a Number. From MDN: Unary + can:
convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.
Upvotes: 13
Reputation: 624
There is a better method which keeps precision and also strips the zeros. This takes an input number and through some magic of casting will pull off any trailing zeros. I've found 16 to be the precision limit for me which is pretty good should you not be putting a satellite on pluto.
function convertToFixed(inputnum)
{
var mynum = inputnum.toPrecision(16);
//If you have a string already ignore this first line and change mynum.toString to the inputnum
var mynumstr = mynum.toString();
return parseFloat(mynumstr);
}
alert(convertToFixed(6.6/6));
Upvotes: 0
Reputation: 273
None of these really got me what I was looking for based on the question title, which was, for example, for 5.00 to be 5 and 5.10 to be 5.1. My solution was as follows:
num.toFixed(places).replace(/\.?0+$/, '')
'5.00'.replace(/\.?0+$/, '') // 5
'5.10'.replace(/\.?0+$/, '') // 5.1
'5.0000001'.replace(/\.?0+$/, '') // 5.0000001
'5.0001000'.replace(/\.?0+$/, '') // 5.0001
Note: The regex only works if places > 0
P.S. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Upvotes: 4
Reputation: 104760
Number(n.toFixed(15)) or +(n.toFixed(15))
will convert the 15 place decimal string to a number, removing trailing zeroes.
Upvotes: 16
Reputation: 9292
As I understand, you want to remove the trailing zeros in the string that you obtained via toFixed()
. This is a pure string operation:
var x = 1.1230000;
var y = x.toFixed(15).replace(/0+$/, ""); // ==> 1.123
Upvotes: 25
Reputation: 15765
Yes, there is a way. Use parseFloat()
.
parseFloat((1.005).toFixed(15)) //==> 1.005
parseFloat((1.000000000).toFixed(15)) //==> 1
See a live example here: http://jsfiddle.net/nayish/7JBJw/
Upvotes: 43
Reputation: 7349
>>> parseFloat(0.9999999.toFixed(4));
1
>>> parseFloat(0.0009999999.toFixed(4));
0.001
>>> parseFloat(0.0000009999999.toFixed(4));
0
Upvotes: 185