Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 如何格式化干净的数字,使1000显示为1.000,00_Javascript_Regex - Fatal编程技术网

Javascript 如何格式化干净的数字,使1000显示为1.000,00

Javascript 如何格式化干净的数字,使1000显示为1.000,00,javascript,regex,Javascript,Regex,我目前有一些清晰的数字,如: 1、10、100、1000、10000等 我希望格式化这些数字,使其显示为: 1,00 10,00 100,00 1.000,00 10.000,00 我玩了n.toFixed(2)但似乎不支持我正在寻找的格式 有什么想法吗?我不知道你的情况,但我想用 它正好满足您的需求,并且有更多额外的动态格式 更新:特别关注本地化功能。如果你需要它,它甚至可以提供多元化服务。我不知道你的情况如何,但我想使用它 它正好满足您的需求,并且有更多额外的动态格式 更新:特别关注本地化

我目前有一些清晰的数字,如:
1、10、100、1000、10000等

我希望格式化这些数字,使其显示为:

1,00
10,00
100,00
1.000,00
10.000,00
我玩了
n.toFixed(2)但似乎不支持我正在寻找的格式


有什么想法吗?

我不知道你的情况,但我想用

它正好满足您的需求,并且有更多额外的动态格式


更新:特别关注本地化功能。如果你需要它,它甚至可以提供多元化服务。

我不知道你的情况如何,但我想使用它

它正好满足您的需求,并且有更多额外的动态格式


更新:特别关注本地化功能。如果您需要,它甚至可以提供复数形式。

JS中没有内置的格式化数字的方法。您可以为此使用自动数字

JS中没有内置的格式化数字的方法。您可以为此使用自动数字

您可以这样做:

var nb='1000000000';
var result = nb.replace(/(?:(^\d{1,3})(?=(?:\d{3})*$)|(\d{3}))(?!$)/mg, '$1$2.')+',00';

您可以这样做:

var nb='1000000000';
var result = nb.replace(/(?:(^\d{1,3})(?=(?:\d{3})*$)|(\d{3}))(?!$)/mg, '$1$2.')+',00';

查看Intl.NumberFormat(),无需任何花式裤子插件。跨平台支持

var number = 3500;
alert(new Intl.NumberFormat().format(number));

将弹出“3500”

查看Intl.NumberFormat(),无需花式裤子插件。跨平台支持

var number = 3500;
alert(new Intl.NumberFormat().format(number));

将弹出“3500”

如果您想要一个简单的功能来完成此项工作,以下可能适合:

// Format a number n using: 
//   p decimal places (two by default)
//   ts as the thousands separator (comma by default) and
//   dp as the  decimal point (period by default).
//
//   If p < 0 or p > 20 results are implementation dependent.
function formatNumber(n, p, ts, dp) {
  var t = [];
  // Get arguments, set defaults
  if (typeof p  == 'undefined') p  = 2;
  if (typeof ts == 'undefined') ts = ',';
  if (typeof dp == 'undefined') dp = '.';

  // Get number and decimal part of n
  n = Number(n).toFixed(p).split('.');

  // Add thousands separator and decimal point (if requied):
  for (var iLen = n[0].length, i = iLen? iLen % 3 || 3 : 0, j = 0; i <= iLen; i+=3) {
    t.push(n[0].substring(j, i));
    j = i;
  }
  // Insert separators and return result
  return t.join(ts) + (n[1]? dp + n[1] : '');
}


//*
console.log(formatNumber(
    1234567890.567,  // value to format
                 4,  // number of decimal places
               '.',  // thousands separator
                ','  // decimal separator
 ));                 // result: 1.234.567.890,5670
//*/

console.log(formatNumber(
           123.567,  // value to format
                 1   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
         '123.567',  // value to format
                 0   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
               123,  // value to format
                 0   // number of decimal places
 ));                 // result: 123

console.log(formatNumber(
                13,  // value to format
                 2   // number of decimal places
 ));                 // result: 13.00

console.log(formatNumber(
                 0   // value to format
                     // number of decimal places
 ));                 // result: 0.00

console.log(formatNumber(
                     // value to format
                     // number of decimal places
 ));                 // result: NaN
//使用以下格式设置数字n的格式:
//p小数位(默认为两位)
//ts作为千位分隔符(默认为逗号)和
//dp作为小数点(默认为句点)。
//
//如果p<0或p>20,结果取决于实施。
函数格式编号(n、p、ts、dp){
var t=[];
//获取参数,设置默认值
如果(typeof p==‘未定义’)p=2;
如果(typeof ts=='未定义')ts=',';
如果(typeof dp=='未定义')dp=';
//获取n的数字和小数部分
n=数量(n).toFixed(p).split('.');
//添加千位分隔符和小数点(如果需要):

对于(var-iLen=n[0].length,i=iLen?iLen%3 | | 3:0,j=0;i如果需要一个简单的函数来完成此任务,以下可能适合:

// Format a number n using: 
//   p decimal places (two by default)
//   ts as the thousands separator (comma by default) and
//   dp as the  decimal point (period by default).
//
//   If p < 0 or p > 20 results are implementation dependent.
function formatNumber(n, p, ts, dp) {
  var t = [];
  // Get arguments, set defaults
  if (typeof p  == 'undefined') p  = 2;
  if (typeof ts == 'undefined') ts = ',';
  if (typeof dp == 'undefined') dp = '.';

  // Get number and decimal part of n
  n = Number(n).toFixed(p).split('.');

  // Add thousands separator and decimal point (if requied):
  for (var iLen = n[0].length, i = iLen? iLen % 3 || 3 : 0, j = 0; i <= iLen; i+=3) {
    t.push(n[0].substring(j, i));
    j = i;
  }
  // Insert separators and return result
  return t.join(ts) + (n[1]? dp + n[1] : '');
}


//*
console.log(formatNumber(
    1234567890.567,  // value to format
                 4,  // number of decimal places
               '.',  // thousands separator
                ','  // decimal separator
 ));                 // result: 1.234.567.890,5670
//*/

console.log(formatNumber(
           123.567,  // value to format
                 1   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
         '123.567',  // value to format
                 0   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
               123,  // value to format
                 0   // number of decimal places
 ));                 // result: 123

console.log(formatNumber(
                13,  // value to format
                 2   // number of decimal places
 ));                 // result: 13.00

console.log(formatNumber(
                 0   // value to format
                     // number of decimal places
 ));                 // result: 0.00

console.log(formatNumber(
                     // value to format
                     // number of decimal places
 ));                 // result: NaN
//使用以下格式设置数字n的格式:
//p小数位(默认为两位)
//ts作为千位分隔符(默认为逗号)和
//dp作为小数点(默认为句点)。
//
//如果p<0或p>20,结果取决于实施。
函数格式编号(n、p、ts、dp){
var t=[];
//获取参数,设置默认值
如果(typeof p==‘未定义’)p=2;
如果(typeof ts=='未定义')ts=',';
如果(typeof dp=='未定义')dp=';
//获取n的数字和小数部分
n=数量(n).toFixed(p).split('.');
//添加千位分隔符和小数点(如果需要):


对于(var-iLen=n[0]。length,i=iLen?iLen%3 | | | 3:0,j=0;i-JS没有本机的“花式”数字格式函数。您必须使用自己的函数,或者找到一个类似于模拟sprintf()的预制库第三方脚本是可以接受的解决方案吗?任何东西都可以接受,目前我不知道怎么做-但这是一个小脚本,所以突然必须包含其他大型库是不可能的-但我洗耳恭听解决方案:)有你要找的答案吗。JS没有本地的“想象力”数字格式化函数。您必须自己使用,或者找到一个预制作的库,它可以模拟sprintf()第三方脚本是可以接受的解决方案吗?任何东西都可以接受,目前我不知道如何做到这一点-但这是一个小脚本,所以突然必须包含其他大型库将是不可能的-但我洗耳恭听解决方案:)有你想要的答案。除非你指出具体的功能,否则它将是-1。加载Angular进行数字格式设置是多余的。@KarolyHorvath嘿,我已经完成了你的要求,我只想贡献一点。@Jasen就像我说的,我不知道他的情况是什么。我在另一个解决方案中研究Angular很快,我们就来看看这个,所以很高兴知道这个选项。Thanksit将是一个-1,除非你指出具体的功能来实现这一点。而且加载数字格式的角度是多余的。@KarolyHorvath嘿,我已经完成了你的要求,我只想贡献一点。@Jasen就像我说的,我不知道他的是什么情况是这样的。我很快会在另一个解决方案中研究Angular,所以到时候只需看一眼这个,所以知道那里的选项总是很好。谢谢您可以提供
locales
参数,并根据OP的需要交换逗号和小数。
Intl.NumberFormat(“de-de”)
不幸的是,糟糕的浏览器支持并没有那么好…既然你链接了Mozilla文档,我就冒昧地猜测Firefox也受支持了?从Mozilla链接:Firefox(Gecko)、Safari(Webkit)-不支持您还可以提供
locales
参数,以便根据OP的需要交换逗号和小数。
Intl.NumberFormat(“de-de”)
不幸的是,糟糕的浏览器支持并没有那么好…既然你链接了Mozilla文档,我就冒昧地猜测Firefox也受支持了?从Mozilla链接:Firefox(Gecko)、Safari(Webkit)-不支持我喜欢有用的东西!这是一个很好的例子,它能做我需要它做的事情。第一个console.log示例为我做到了。我爱你!:)我喜欢有用的东西!这是一个很好的例子,它能做我需要它做的事情。第一个console.log示例为我做到了。我爱你!:)