简体   繁体   English

Javascript:使用 toLocaleString + Tofixed

[英]Javascript: using toLocaleString + Tofixed

I need to format a number for a project im working on at work, only problem is that I cant format it how i want.我需要为我正在工作的项目格式化一个数字,唯一的问题是我无法按照我想要的方式格式化它。

I convert the number to a localestring using the toLocaleString method which gives me the commas but i also need decimal places, nothing i seem to do works.我使用toLocaleString方法将数字转换为语言环境字符串,该方法给了我逗号,但我也需要小数位,我似乎没有做任何工作。

var number = 123.322
number = parseFloat(number).toFixed(2) //123.22
number.toLocaleString() //123.22

The above code just returns the parsefloated number along with the tofixed decimal values but it doesn't add the commas.上面的代码只返回解析浮点数以及固定十进制值,但不添加逗号。

How do i get a number to have two decimal places (when the value is 'xx.00') and also be comma separated.我如何让一个数字有两个小数位(当值为 'xx.00' 时)并且也用逗号分隔。 Is this possible in JavaScript?这在 JavaScript 中可能吗?

You can give an object to .toLocaleString() which describes what you want:您可以为.toLocaleString()提供一个对象,该对象描述您想要的内容:

var sNumber = (10123.322).toLocaleString(undefined,
 {'minimumFractionDigits':2,'maximumFractionDigits':2});

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString文档: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Original:原文:

 const fNumber = 10123.322; const sNumber = parseFloat(fNumber.toFixed(2)).toLocaleString(); console.log(sNumber);

The number is already in decimal/float format on the first line.第一行的数字已经是十进制/浮点格式。

  • .toFixed(2) turns it into a string using fixed-point notation. .toFixed(2)使用定点表示法将其转换为字符串。
  • parseFloat() takes that string and turns it back into a float. parseFloat()获取该字符串并将其转换回浮点数。
  • .toLocaleString() turns it into a string using the local format. .toLocaleString()使用本地格式将其转换为字符串。

Just to do it in one line只需在一行中完成

var num = '12233.3366554';
num = parseFloat(parseFloat(num).toFixed(2)).toLocaleString('en-IN', { useGrouping: true });

Number.toLocaleString works on a number, but toFixed returns a string. Number.toLocaleString适用于数字,但toFixed返回一个字符串。

Coerce the string back into a number first先将字符串强制回数字

 var number = 123.322; var string = parseFloat(number).toFixed(2); var parsed = (+string).toLocaleString(); console.log(parsed);

In order to get commas you have to specify the locale .The locale en includes commas for the numbers.为了获得逗号,您必须指定语言环境。语言环境包括数字的逗号。 toFixed() Returns a string . toFixed() Returns a string toLocaleString() function commas works on a number not on a string.So parse the string to float. toLocaleString() 函数逗号适用于数字而不是字符串。因此将字符串解析为浮点数。

var number = 1234567.322;
number = parseFloat(parseFloat(number).toFixed(2)).toFixed(2) ;
number=number.toLocaleString('en'); 

toLocaleString function provide number representation based on languages toLocaleString 函数提供基于语言的数字表示

 var number = 3500.00;

if(Number.isInteger(number)){

   var zeroappend= number.toLocaleString()+".00";
   console.log(zeroappend);//3,500.00;


}else{
   console.log(number.toLocaleString()); 
}

Yes, it is possible using .toLocaleString, yo just need to specify the language, optionally you can specify decimals and currency.是的,可以使用 .toLocaleString,您只需要指定语言,您可以选择指定小数和货币。 look at this example:看看这个例子:

35000.2455.toLocaleString('en-IN', {minimumFractionDigits: 2, maximumFractionDigits: 2,style: 'currency', currency: 'USD' })

this returns $35,000.25这将返回$35,000.25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM