If you want to remove same number of trailing zeros from numbers, you can apply a formula to solve it. Select a adjacent cell to the number you use, type this formula =LEFT(D1,LEN(D1)-3)*1, D1 is the cell you will remove trailing zeros from, 3 is the number or zeros you want to remove.
To remove the trailing zeros from a number, pass the number to the parseFloat() function. The parseFloat function parses the provided value, returning a floating point number, which automatically removes any trailing zeros.
Trailing zeros are not significant in numbers without decimal points. indicate that the trailing zeros are significant a decimal point must be added.
I had a similar instance where I wanted to use .toFixed()
where necessary, but I didn't want the padding when it wasn't. So I ended up using parseFloat in conjunction with toFixed.
toFixed without padding
parseFloat(n.toFixed(4));
Another option that does almost the same thing
This answer may help your decision
Number(n.toFixed(4));
toFixed
will round/pad the number to a specific length, but also convert it to a string. Converting that back to a numeric type will not only make the number safer to use arithmetically, but also automatically drop any trailing 0's. For example:
var n = "1.234000";
n = parseFloat(n);
// n is 1.234 and in number form
Because even if you define a number with trailing zeros they're dropped.
var n = 1.23000;
// n == 1.23;
If you convert it to a string it will not display any trailing zeros, which aren't stored in the variable in the first place since it was created as a Number, not a String.
var n = 1.245000
var noZeroes = n.toString() // "1.245"
I first used a combination of matti-lyra and gary's answers:
r=(+n).toFixed(4).replace(/\.0+$/,'')
Results:
The somewhat problematic case is 0.10001. I ended up using this longer version:
r = (+n).toFixed(4);
if (r.match(/\./)) {
r = r.replace(/\.?0+$/, '');
}
Update: And this is Gary's newer version (see comments):
r=(+n).toFixed(4).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/,'$1')
This gives the same results as above.
The toFixed
method will do the appropriate rounding if necessary. It will also add trailing zeroes, which is not always ideal.
(4.55555).toFixed(2);
//-> "4.56"
(4).toFixed(2);
//-> "4.00"
If you cast the return value to a number, those trailing zeroes will be dropped. This is a simpler approach than doing your own rounding or truncation math.
+(4.55555).toFixed(2);
//-> 4.56
+(4).toFixed(2);
//-> 4
How about just multiplying by one like this?
var x = 1.234000*1; // becomes 1.234
var y = 1.234001*1; // stays as 1.234001
I had the basically the same requirement, and found that there is no built-in mechanism for this functionality.
In addition to trimming the trailing zeros, I also had the need to round off and format the output for the user's current locale (i.e. 123,456.789).
All of my work on this has been included as prettyFloat.js (MIT Licensed) on GitHub: https://github.com/dperish/prettyFloat.js
Usage Examples:
prettyFloat(1.111001, 3) // "1.111"
prettyFloat(1.111001, 4) // "1.111"
prettyFloat(1.1111001, 5) // "1.1111"
prettyFloat(1234.5678, 2) // "1234.57"
prettyFloat(1234.5678, 2, true) // "1,234.57" (en-us)
Updated - August, 2018
All modern browsers now support the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting.
let formatters = {
default: new Intl.NumberFormat(),
currency: new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0, maximumFractionDigits: 0 }),
whole: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 0, maximumFractionDigits: 0 }),
oneDecimal: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 1, maximumFractionDigits: 1 }),
twoDecimal: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 })
};
formatters.twoDecimal.format(1234.5678); // result: "1,234.57"
formatters.currency.format(28761232.291); // result: "$28,761,232"
For older browsers, you can use this polyfill: https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With