将整数转换为带有逗号的字符串(以千为单位)
我想将整数 35634646 转换为千位“,”,因此它应该是 35,634,646。
最快的方法是什么?
I want to convert an Integer 35634646 to have the thousand "," so it should be 35,634,646.
What would be the quickest way to doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
其他答案是正确的,但是在使用
"%,d"
之前请仔细检查您的区域设置:结果:
The other answers are correct, however double-check your locale before using
"%,d"
:Result:
使用扩展
结果
use Extension
result
这个解决方案对我有用:
This solution worked for me:
使用带有逗号的
%d
格式说明符:%,d
这是迄今为止最简单的方法。
Use the
%d
format specifier with a comma:%,d
This is by far the easiest way.
对于那些无法访问“numberformat”或“String.format”(在框架内使用有限版本的java)的人来说,这是一个解决方案。希望它有用。
here's a solution for those of you who can't access "numberformat" nor "String.format" (using a limited version of java inside a framework). Hope it's useful.
如果必须在 JSP 中执行相同操作,请使用:
当然,对于多个值,请使用:
If the same has to be done in the JSP , use:
ofcourse for multiple values use :
这种方法还可以让您用任何字符替换默认分隔符:
This is a way that also able you to replace default separator with any characters:
你不能使用
printf
中的逗号吗?应该将逗号添加到%d
中间。对此并不积极,但对我有用。
can't you use a
The comma in the
printf
should add the commas into the%d
inter.Not positive about it, but works for me.
首先,您需要
在页面开头
包含 JSTL 标签:-
First you need to include the JSTL tags :-
at the start of the page
您要求最快,但也许您的意思是“最好”或“正确”或“典型”?
您还要求用逗号来表示数千,但也许您的意思是“根据用户的当地习惯以正常的人类可读形式”?
你这样做:
美国人将得到“35,634,646”
德国人将得到“35.634.646”
瑞士德国人将得到“35'634'646”
You ask for quickest, but perhaps you mean "best" or "correct" or "typical"?
You also ask for commas to indicate thousands, but perhaps you mean "in normal human readable form according to the local custom of your user"?
You do it as so:
Americans will get "35,634,646"
Germans will get "35.634.646"
Swiss Germans will get "35'634'646"
整数:
双精度数:
String.format 非常强大。
-
根据 psuzzi 反馈进行编辑。
Integers:
Doubles:
String.format is pretty powerful.
-
Edited per psuzzi feedback.
输出:
35,634,646
Output:
35,634,646