Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果是整数,则追加,00_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如果是整数,则追加,00

Javascript 如果是整数,则追加,00,javascript,jquery,html,Javascript,Jquery,Html,我需要附加一个小数点,但有一个昏迷,所以如果是300,它应该改为300,00,我有下面的代码,它可以工作,但如果数字已经是一个小数点,它会改为NAN。我需要代码不要修改已经是小数的数字 var formatter=new Intl.NumberFormat'es-es'{ 风格:“货币”, 货币:美元, }; $'.total amount'.eachfunction{ var x=$this.text.replace/[^\d,]/g; $this.textformatter.formatx

我需要附加一个小数点,但有一个昏迷,所以如果是300,它应该改为300,00,我有下面的代码,它可以工作,但如果数字已经是一个小数点,它会改为NAN。我需要代码不要修改已经是小数的数字

var formatter=new Intl.NumberFormat'es-es'{ 风格:“货币”, 货币:美元, }; $'.total amount'.eachfunction{ var x=$this.text.replace/[^\d,]/g; $this.textformatter.formatx; }; 总数为: 以十进制为例:

$ 300,00 无小数点示例:

$ 300 您可以使用includes检查文本中是否已有逗号

var formatter=new Intl.NumberFormat'es-es'{ 风格:“货币”, 货币:美元, }; $'.total amount'.eachfunction{ 让text=$this.text; if!text.includes','{ var x=text.replace/[^\d,]/g; $this.textformatter.formatx; } }; 总数为: $ 12,200
我会这样做:

const formatCurrency = function (number) {
   const f = new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'USD' });
   if (number != parseInt(number, 10)) {
      return number;
   }
   return f.format(number)
}
并以同样的方式使用它:

$('.total-price').each(function() {
    var x = $(this).text().replace(/[^\d,]/g, "");
    $(this).text(formatCurrency(x));
});

这样,您将仅在数字为整数时格式化该数字,否则返回未修改的数字

您只需再添加一个replace来替换后面的文本

例如:

var formatter=new Intl.NumberFormat'es-es'{ 风格:“货币”, 货币:美元, }; $'.total amount'.eachfunction{ var x=$this.text.replace/[^\d,]/g、.replace/,.*$/g/*