javascript 如果一个数字有 .00 你怎么能删除它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17169404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:21:07  来源:igfitidea点击:

if a number has .00 how can you remove it?

javascriptnumbersdecimal

提问by user1943465

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.

我在一个电子商务网站上工作,如果它是整数,他们的产品价格会自动上传“.00”。我们有一些产品会显示使用的小数点(即 £13.50),但我们只想从定价中删除 .00 的所有实例。

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.

如果有人需要更多信息,请告诉我。我知道定价来自网站的许多部分,而不仅仅是某一特定课程。

回答by Denys Séguret

If you onlywant to remove the .00if it's the end of a string, then you may do

如果你只是想删除的.00,如果它是一个字符串的结尾,那么你可以这样做

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

如果您的 .00 可能不在字符串的末尾,例如它是"10.00 $ and 24.00",那么请执行

str = str.replace(/\.00([^\d])/g,'');


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.

因此,如果您对我给您的解决方案不满意,请花时间定义您的完整要求。

回答by sakhunzai

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

回答by Zaheer Ahmed

working demoHere is code:

工作演示这是代码:

var amount=23.00;
if(amount % 1 == 0)
   amount=parseInt(amount,10);

回答by andlrc

You can use parseFloat: This will push the variable to a number and only keep the necessary decimals

您可以使用parseFloat:这会将变量推到一个数字并只保留必要的小数

parseFloat('10.50'); // 10.5
parseFloat('10.00'); // 10
parseFloat('99.95000000'); // 95.95

回答by Vikram Sharma

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

使用 parseInt 与原始数进行比较,如果原始数更大,则保持原样,否则使用 parseInt 的结果

回答by Prince Prasad

Just multiply the number with 1

只需将数字乘以 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);

回答by Sbbs

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 replaceon it. You can convert a float number to string with toFixed(2)(for currencies with 2 decimals):

在调用replace它之前,请确保您的货币是一个字符串。您可以将浮点数转换为字符串toFixed(2)(对于具有 2 个小数的货币):

12.50.toFixed(2) // => "12.50"

回答by Mohit

One way is to use a number_format( number, decimals, dec_point, thousands_sep )

一种方法是使用 number_format(number,decimals,dec_point,千分之一)

number_format( 123456.00, 0, ".", "" ); //result  123456

It is not native to javascript, but has been built to mirror the PHP number_format function.

它不是 javascript 原生的,但已构建以反映 PHP number_format 函数。

http://phpjs.org/functions/number_format/

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' 
}