将整数转换为带有逗号的字符串(以千为单位)

发布于 2024-11-29 17:00:25 字数 67 浏览 1 评论 0原文

我想将整数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

绾颜 2024-12-06 17:00:26

其他答案是正确的,但是在使用 "%,d" 之前请仔细检查您的区域设置:

Locale.setDefault(Locale.US);
int bigNumber = 35634646;
String formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Locale.setDefault(new Locale("pl", "PL"));
formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

结果:

35,634,646
35 634 646

The other answers are correct, however double-check your locale before using "%,d":

Locale.setDefault(Locale.US);
int bigNumber = 35634646;
String formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Locale.setDefault(new Locale("pl", "PL"));
formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Result:

35,634,646
35 634 646
傾城如夢未必闌珊 2024-12-06 17:00:26

使用扩展

import java.text.NumberFormat

val Int.commaString: String
  get() = NumberFormat.getInstance().format(this)

val String.commaString: String
  get() = NumberFormat.getNumberInstance().format(this.toDouble())

val Long.commaString: String
  get() = NumberFormat.getInstance().format(this)

val Double.commaString: String
  get() = NumberFormat.getInstance().format(this)

结果

1234.commaString => 1,234
"1234.456".commaString => 1,234.456
1234567890123456789.commaString => 1,234,567,890,123,456,789
1234.456.commaString => 1,234.456

use Extension

import java.text.NumberFormat

val Int.commaString: String
  get() = NumberFormat.getInstance().format(this)

val String.commaString: String
  get() = NumberFormat.getNumberInstance().format(this.toDouble())

val Long.commaString: String
  get() = NumberFormat.getInstance().format(this)

val Double.commaString: String
  get() = NumberFormat.getInstance().format(this)

result

1234.commaString => 1,234
"1234.456".commaString => 1,234.456
1234567890123456789.commaString => 1,234,567,890,123,456,789
1234.456.commaString => 1,234.456
倾城月光淡如水﹏ 2024-12-06 17:00:26

这个解决方案对我有用:

NumberFormat.getNumberInstance(Locale.US).format(Integer.valueOf("String Your Number"));

This solution worked for me:

NumberFormat.getNumberInstance(Locale.US).format(Integer.valueOf("String Your Number"));
魄砕の薆 2024-12-06 17:00:26

使用带有逗号的 %d 格式说明符: %,d

这是迄今为止最简单的方法。

Use the %d format specifier with a comma: %,d

This is by far the easiest way.

姜生凉生 2024-12-06 17:00:26

对于那些无法访问“numberformat”或“String.format”(在框架内使用有限版本的java)的人来说,这是一个解决方案。希望它有用。

number= 123456789;
thousandsSeparator=",";
myNumberString=number.toString();   
numberLength=myNumberString.length;
howManySeparators=Math.floor((numberLength-1)/3)
formattedString=myNumberString.substring(0,numberLength-(howManySeparators*3))
while (howManySeparators>0)    {
formattedString=formattedString+thousandsSeparator+myNumberString.substring(numberLength-(howManySeparators*3),numberLength-((howManySeparators-1)*3));
howManySeparators=howManySeparators-1;    }

formattedString

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.

number= 123456789;
thousandsSeparator=",";
myNumberString=number.toString();   
numberLength=myNumberString.length;
howManySeparators=Math.floor((numberLength-1)/3)
formattedString=myNumberString.substring(0,numberLength-(howManySeparators*3))
while (howManySeparators>0)    {
formattedString=formattedString+thousandsSeparator+myNumberString.substring(numberLength-(howManySeparators*3),numberLength-((howManySeparators-1)*3));
howManySeparators=howManySeparators-1;    }

formattedString
踏雪无痕 2024-12-06 17:00:26

如果必须在 JSP 中执行相同操作,请使用:

<fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
<c:out value="${formattedVariable}"></c:out>

当然,对于多个值,请使用:

<c:forEach items="${yourlist}" var="yourlist">

    <fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
    <c:out value="${formattedVariable}"></c:out>
</c:forEach>

If the same has to be done in the JSP , use:

<fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
<c:out value="${formattedVariable}"></c:out>

ofcourse for multiple values use :

<c:forEach items="${yourlist}" var="yourlist">

    <fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
    <c:out value="${formattedVariable}"></c:out>
</c:forEach>
唐婉 2024-12-06 17:00:26

这种方法还可以让您用任何字符替换默认分隔符:

val myNumber = NumberFormat.getNumberInstance(Locale.US)
   .format(123456789)
   .replace(",", "،")

This is a way that also able you to replace default separator with any characters:

val myNumber = NumberFormat.getNumberInstance(Locale.US)
   .format(123456789)
   .replace(",", "،")
蓝海 2024-12-06 17:00:26
/**
@inpute 100000
@return 1,000,000
*/
    public static String addComma(long amount) {
        return NumberFormat.getNumberInstance(Locale.US).format(amount);
    }
/**
@inpute 100000
@return 1,000,000
*/
    public static String addComma(long amount) {
        return NumberFormat.getNumberInstance(Locale.US).format(amount);
    }
-残月青衣踏尘吟 2024-12-06 17:00:26

你不能使用

System.out.printf("%n%,d",int name);

printf中的逗号吗?应该将逗号添加到%d中间。

对此并不积极,但对我有用。

can't you use a

System.out.printf("%n%,d",int name);

The comma in the printf should add the commas into the %d inter.

Not positive about it, but works for me.

云仙小弟 2024-12-06 17:00:26

首先,您需要

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

在页面开头

包含 JSTL 标签:-

First you need to include the JSTL tags :-

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

at the start of the page

半窗疏影 2024-12-06 17:00:25
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646
狼亦尘 2024-12-06 17:00:25

您要求最快,但也许您的意思是“最好”或“正确”或“典型”?

您还要求用逗号来表示数千,但也许您的意思是“根据用户的当地习惯以正常的人类可读形式”?

你这样做:

    int i = 35634646;
    String s = NumberFormat.getIntegerInstance().format(i);

美国人将得到“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:

    int i = 35634646;
    String s = NumberFormat.getIntegerInstance().format(i);

Americans will get "35,634,646"

Germans will get "35.634.646"

Swiss Germans will get "35'634'646"

在你怀里撒娇 2024-12-06 17:00:25
int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);
int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);
清醇 2024-12-06 17:00:25

整数:

int value = 100000; 
String.format("%,d", value); // outputs 100,000

双精度数:

double value = 21403.3144d;
String.format("%,.2f", value); // outputs 21,403.31

String.format 非常强大。

-
根据 psuzzi 反馈进行编辑。

Integers:

int value = 100000; 
String.format("%,d", value); // outputs 100,000

Doubles:

double value = 21403.3144d;
String.format("%,.2f", value); // outputs 21,403.31

String.format is pretty powerful.

-
Edited per psuzzi feedback.

红玫瑰 2024-12-06 17:00:25
 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

输出:35,​​634,646

 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

Output: 35,634,646

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文