带逗号的java数字字符串
我在解决此问题时遇到问题:
Write a structures program that can accept two integer numbers up to 40 digit and perform the following:
- add the two numbers together and display the result
- the result number should should be seperated by commas.
所以我可以使用BigInteger
做第一件事,但是对于第二部分,我遇到了一些问题。我不知道应该如何在字符串中添加逗号,我使用了for
循环来处理split,但它不起作用
我想起来了谢谢你的帮助
public static String NewString (String num)
{
String sent = "" ;
int count = 0;
for ( int index = num.length()-1 ; index >= 0 ; index --)
{
count++;
sent = num.charAt(index) + sent;
if(count % 3 == 0 && index != 0)
sent = "," + sent;
}
return sent;
}
# 1 楼答案
你可以用
或者你也可以自己写。这将非常简单,只需将
BigInteger
转换为String
,然后向后循环,每传递第三个字符添加一个逗号# 2 楼答案
以下代码:
这是不言自明的,希望这有帮助:)