如何在数字字符串中插入逗号以分隔零?
How can I insert commas in number strings to separate the zeros?
public class test
{
public static void main(String[] args)
{
String abra = "100000";
abra = abra.replace(abra.substring(abra.length() - 3),
"," + abra.substring(abra.length() - 3));
System.out.println(abra);
}
}
完全是 java 的新手,我正在尝试用逗号替换数字以将零分隔为三个。它适用于成千上万的人,但我得到了更多的奇怪结果。有任何想法吗?
提前致谢!
因为你手动输入的只是6位数字。更好的方法是使用 NumberFormat#getNumberInstance
with Locale.US
:
A Locale object represents a specific geographical, political, or
cultural region. An operation that requires a Locale to perform its
task is called locale-sensitive and uses the Locale to tailor
information for the user. For example, displaying a number is a
locale-sensitive operation— the number should be formatted according
to the customs and conventions of the user's native country, region,
or culture.
NumberFormat.getNumberInstance(Locale.US).format(20000000);
// 20,000,000
public class test
{
public static void main(String[] args)
{
String abra = "100000";
abra = abra.replace(abra.substring(abra.length() - 3),
"," + abra.substring(abra.length() - 3));
System.out.println(abra);
}
}
完全是 java 的新手,我正在尝试用逗号替换数字以将零分隔为三个。它适用于成千上万的人,但我得到了更多的奇怪结果。有任何想法吗?
提前致谢!
因为你手动输入的只是6位数字。更好的方法是使用 NumberFormat#getNumberInstance
with Locale.US
:
A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation— the number should be formatted according to the customs and conventions of the user's native country, region, or culture.
NumberFormat.getNumberInstance(Locale.US).format(20000000);
// 20,000,000