Reputation: 181
I've been looking around for this answer and there are some alternatives. Unfortunately, none of them makes sense to me.
I am working on an e-commerce website where they the product price automatically uploads '.00'
if its a whole number. We have some products that will display the decimals where used (i.e £13.50
) but we just want all instances of .00
removed from pricing.
Do let me know if anyone needs any more information on this. I know the pricing comes from many parts of the website and not just one particular class.
Upvotes: 18
Views: 34784
Reputation: 75
you can just use +
console.log(+'12.00')
console.log(+'6.50')
console.log(+'6.99')
Upvotes: 1
Reputation: 14470
Simplest:
Number(1.05) => 1.05
Number(1.00) => 1
Number('1.05') => 1.05
Number('1.00') => 1
OR
parseFloat('102.00 dollar') => 102
parseInt('102.02 dollar') => 102 see it drops .02
parseFloat('102.02 dollar') => 102.02 , but not this
Upvotes: 19
Reputation: 1670
The following solution works with currency on either side:
"£12.00".replace(/\.00/g, '') // => "£12"
"12.00€".replace(/\.00/g, '') // => "12€"
"£12.50".replace(/\.00/g, '') // => "£12.50"
Just make sure your currency is a String before calling replace
on it. You can convert a float number to string with toFixed(2)
(for currencies with 2 decimals):
12.50.toFixed(2) // => "12.50"
Upvotes: 4
Reputation: 1668
Just multiply the number with 1
var x = 1.878000 * 1; // becomes 1.878
document.write(x);
document.write("<br>");
var y = 1.656001 * 1; // stays as 1.656001
document.write(y);
Upvotes: 0
Reputation: 1
One way is to use a number_format( number, decimals, dec_point, thousands_sep )
number_format( 123456.00, 0, ".", "" ); //result 123456
It is not native to javascript, but has been built to mirror the PHP number_format function.
http://phpjs.org/functions/number_format/
In principle, the code taken from the link, does:
function number_format( number, decimals, dec_point, thousands_sep ) {
number = (number + '')
.replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function(n, prec) {
var k = Math.pow(10, prec);
return '' + (Math.round(n * k) / k)
.toFixed(prec);
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
.split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '')
.length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1)
.join('0');
}
return s.join(dec);
// example 1: number_format(1234.56);
// returns 1: '1,235'
// example 2: number_format(1234.56, 2, ',', ' ');
// returns 2: '1 234,56'
// example 3: number_format(1234.5678, 2, '.', '');
// returns 3: '1234.57'
// example 4: number_format(67, 2, ',', '.');
// returns 4: '67,00'
// example 5: number_format(1000);
// returns 5: '1,000'
// example 6: number_format(67.311, 2);
// returns 6: '67.31'
// example 7: number_format(1000.55, 1);
// returns 7: '1,000.6'
// example 8: number_format(67000, 5, ',', '.');
// returns 8: '67.000,00000'
// example 9: number_format(0.9, 0);
// returns 9: '1'
// example 10: number_format('1.20', 2);
// returns 10: '1.20'
// example 11: number_format('1.20', 4);
// returns 11: '1.2000'
// example 12: number_format('1.2000', 3);
// returns 12: '1.200'
// example 13: number_format('1 000,50', 2, '.', ' ');
// returns 13: '100 050.00'
// example 14: number_format(1e-8, 8, '.', '');
// returns 14: '0.00000001'
}
Upvotes: -1
Reputation: 382102
If you only want to remove the .00
if it's the end of a string, then you may do
str = str.replace(/\.00$/,'');
If your .00 may not be at the end of your string, for example it's "10.00 $ and 24.00€"
, then do
str = str.replace(/\.00([^\d])/g,'$1');
What follows here is more a comment :
Most often, number formatting involve many other requirements, we can't guess what are yours.
Here's for example the test set of one of my number formatting functions :
// formatFloat(100.0) => "100"
// formatFloat(100.0, 12) => "100"
// formatFloat(100.1) => "100.1"
// formatFloat(100.1, 0) => "100"
// formatFloat(100.7, 0) => "101"
// formatFloat(.0434) => "0.043"
// formatFloat(1.999999) => "2"
// formatFloat(.0000047) => "0"
// formatFloat(.0000047, 6) => "0.000005"
// formatFloat(.0000047, 12) => "0.0000047"
// formatFloat(undefined) => ""
// formatFloat(NaN) => ""
So if you're not happy with the solution I gave you, please take the time to define your complete requirement.
Upvotes: 28
Reputation: 47099
You can use parseFloat: This will push the variable to a number and only keep the necessary decimals
parseFloat('10.50'); // 10.5
parseFloat('10.00'); // 10
parseFloat('99.95000000'); // 95.95
Upvotes: 1
Reputation: 28528
working demo Here is code:
var amount=23.00;
if(amount % 1 == 0)
amount=parseInt(amount,10);
Upvotes: 7
Reputation: 387
This can be a solution for your problem:
Use parseInt to compare with the original number, if original number is greater, keep as is otherwise use the result from parseInt
Upvotes: 0