JavaScript 中的数字格式与 C# 类似
有没有一种简单的方法可以在 JavaScript 中格式化数字,类似于 C#(或 VB.NET)中通过 ToString("format_provider")
或 String.Format()?
Is there a simple way to format numbers in JavaScript, similar to the formatting methods available in C# (or VB.NET) via ToString("format_provider")
or String.Format()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
一般
在 jQuery 中
Generally
In jQuery
是的,肯定有一种方法可以在 javascript 中正确格式化数字,例如:
使用variablename.toFixed。
还有另一个函数
toPrecision()
。有关更多详细信息,您还可以访问
http://raovishal.blogspot。 com/2012/01/number-format-in-javascript.html
Yes, there is definitely a way to format numbers properly in javascript, for example:
With the use of variablename.toFixed .
And there is another function
toPrecision()
.For more detail you also can visit
http://raovishal.blogspot.com/2012/01/number-format-in-javascript.html
这是一个简单的 JS 函数,用于向字符串格式的整数添加逗号。它将处理整数或小数。您可以向其传递数字或字符串。显然它返回一个字符串。
下面是一组测试用例: http://jsfiddle.net/jfriend00/6y57j/
可以看到它在之前的 jsFiddle 中使用:http://jsfiddle.net/jfriend00/sMnjT/。通过简单的 Google 搜索“javascript add commas”,您也可以找到处理十进制数字的函数。
将数字转换为字符串可以通过多种方式完成。最简单的方法就是将其添加到字符串中:
在 jsFiddle 的上下文中,您可以通过更改此行将逗号放入计数器中:
对此:
您可以在此处看到它的工作方式: http://jsfiddle.net/jfriend00/CbjSX/
Here's a simple JS function to add commas to an integer number in string format. It will handle whole numbers or decimal numbers. You can pass it either a number or a string. It obviously returns a string.
Here's a set of test cases: http://jsfiddle.net/jfriend00/6y57j/
You can see it being used in this previous jsFiddle: http://jsfiddle.net/jfriend00/sMnjT/. You can find functions that will handle decimal numbers too with a simple Google search for "javascript add commas".
Converting a number to a string can be done many ways. The easiest is just to add it to a string:
Within, the context of your jsFiddle, you'd get commas into the counter by changing this line:
to this:
You can see it working here: http://jsfiddle.net/jfriend00/CbjSX/
我编写了一个简单的函数(不需要另一个 jQuery 插件!!),如果数字不是以数字开头,则将数字转换为十进制分隔的字符串或空字符串:
format(578999); 结果为
578,999
format(10);
结果为10
如果您想要小数点而不是逗号,只需替换逗号即可在带有小数点的代码中。
评论之一正确地指出这仅适用于整数,通过一些小的调整,您可以使其也适用于浮点:
I wrote a simple function (not yet another jQuery plugin needed!!) that converts a number to a decimal separated string or an empty string if the number wasn't a number to begin with:
format(578999);
results in578,999
format(10);
results in10
if you want to have a decimal point instead of a comma simply replace the comma in the code with a decimal point.
One of the comments correctly stated this only works for integers, with a few small adaptions you can make it work for floating points as well:
这里有一些解决方案,都通过了测试套件,包括测试套件和基准测试,如果你想复制粘贴来测试,请尝试 这个要点。
方法 0 (RegExp)
基于 https://stackoverflow.com/a/14428340/1877620,但修复是否有没有小数点。
方法 1
方法 2(拆分为数组)
方法 3(循环)
示例
分隔符
如果我们想要自定义千位分隔符或小数分隔符,请使用 Replace():
测试套件
基准
Here are some solutions, all pass the test suite, test suite and benchmark included, if you want copy and paste to test, try This Gist.
Method 0 (RegExp)
Base on https://stackoverflow.com/a/14428340/1877620, but fix if there is no decimal point.
Method 1
Method 2 (Split to Array)
Method 3 (Loop)
Example
Separator
If we want custom thousands separator or decimal separator, use replace():
Test suite
Benchmark
如果您不想使用 jQuery,请查看 Numeral.js
If you don't want to use jQuery, take a look at Numeral.js
首先,在 JS 中将整数转换为字符串非常简单:
如果你有一个数字作为字符串并希望将其转换为整数,则必须对整数使用
parseInt(string)
parseFloat(string)
用于浮点数。然后,这两个函数都会返回所需的整数/浮点数。示例:您到底想附加什么等,从您的问题中我仍然不清楚。
Firstly, converting an integer into string in JS is really simple:
If you have a number as a string and want it to be turned to an integer, you have to use the
parseInt(string)
for integers andparseFloat(string)
for floats. Both of these functions then return the desired integer/float. Example:What exactly are you trying to append etc, remains unclear to me from your question.
http://code.google.com/p/javascript-number-formatter/:
UPDATE
正如 Tomáš Zato 这里所说的一行解决方案:
ECMA-262 5.1 版本中描述:
并将在未来版本的浏览器中工作...
http://code.google.com/p/javascript-number-formatter/ :
UPDATE
As say Tomáš Zato here one line solution:
which described in ECMA-262 5.1 Edition:
and will work in future versions of browsers...
例如:
For example:
使用JQuery。
Using JQuery.
要获得逗号后有 2 个数字的小数,您可以使用:
To get a decimal with 2 numbers after the comma, you could just use:
我可以建议 numbro 进行基于区域设置的格式化和 numbro 进行基于区域设置的格式化npmjs.com/package/number-format.js" rel="nofollow noreferrer">number-format.js 适用于一般情况。根据用例将两者结合起来可能会有所帮助。
May I suggest numbro for locale based formatting and number-format.js for the general case. A combination of the two depending on use-case may help.
这是另一个版本:
Here's another version:
我做了一个简单的函数,也许有人可以使用它
,它可以生成以下输出:
I made a simple function, maybe someone can use it
this can generate the followings outputs:
如果您想要格式化数字以供查看而不是计算,您可以使用此
输入: {Integer} Number
输出: {String} Number
22,870 =>如果数字 22870
2287 万 =>如果数字 2287xxxx(x 可以是任何值)
228,700 亿 =>如果数字 2287xxxxxxx
22,87 万亿 =>如果号码 2287xxxxxxxxx
你就明白了
In case you want to format number for view rather than for calculation you can use this
Input: {Integer} Number
Outputs: {String} Number
22,870 => if number 22870
22,87 million => if number 2287xxxx (x can be whatever)
22,87 billion => if number 2287xxxxxxx
22,87 trillion => if number 2287xxxxxxxxxx
You get the idea
为了进一步jfriend00的回答(我没有足够的观点来评论),我将他/她的回答扩展到以下内容:
To further jfriend00's answer (I dont't have enough points to comment) I have extended his/her answer to the following:
您可以通过以下方式进行操作:
因此,您不仅可以格式化数字,还可以将要显示的小数位数作为参数传递,您可以设置自定义小数和英里分隔符。
使用:
You can do it in the following way:
So you will not only format the number but you can also pass as a parameter how many decimal digits to display, you set a custom decimal and mile separator.
Use: