15

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.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
user1943465
  • 151
  • 1
  • 1
  • 5
  • Similar question with an elegant solution : https://stackoverflow.com/questions/3612744/remove-insignificant-trailing-zeros-from-a-number – understack Dec 09 '20 at 08:50

9 Answers9

28

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
18

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
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • While Number(10.00) returns 10 as OP requires, Number(10.50) returns 10.5, which may not be ideal in commerce where non-integer prices are better displayed with two decimals. Displaying $10.5 instead of $10.50 would be slightly confusing. So, a solution should return 10.00 as 10 and 10.50 as 10.50, like Denys Séguret's answer. – yenren Aug 30 '19 at 06:50
  • `Number('1000')` returns NaN – Dan Mar 04 '20 at 20:58
7

working demo Here is code:

var amount=23.00;
if(amount % 1 == 0)
   amount=parseInt(amount,10);
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
3

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"
Sbbs
  • 1,610
  • 3
  • 22
  • 34
1

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
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
1

you can just use +

console.log(+'12.00')
console.log(+'6.50')
console.log(+'6.99')
felbsn
  • 11
  • 1
  • 5
0

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

Vikram Sharma
  • 377
  • 1
  • 8
  • 21
0

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);
Prince Prasad
  • 1,528
  • 1
  • 16
  • 20
-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' 
}
user3666197
  • 1
  • 6
  • 50
  • 92
Mohit
  • 1