gpt4 book ai didi

java - 如何在数字中插入逗号?

转载 作者:搜寻专家 更新时间:2023-11-01 02:35:57 24 4
gpt4 key购买 nike

我在一本书中找到了这样一个使用String.format()的例子:

package stringFormat;

public class Main {
public static void main(String[] args) {
String test = String.format("%, d", 1000000000);
System.out.println(test);
}
}

根据这本书,输出应该是:1,000,000,000。但是当我运行代码时,我只得到没有逗号的 1 000 000 000。为什么?我怎样才能用逗号得到它?

output picture

最佳答案

使用 Locale.FRANCE 重现问题:

Locale.setDefault(Locale.FRANCE);

String test = String.format("%, d", 1000000000);
System.out.println(test); // 1 000 000 000

您可以使用 Locale.US 避免这种情况:

String test = String.format(Locale.US, "%, d", 1000000000);

or

Locale.setDefault(Locale.US);
String test = String.format("%, d", 1000000000);

关于java - 如何在数字中插入逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com