【问题标题】:Separating BigInteger result by comma [duplicate]用逗号分隔 BigInteger 结果[重复]
【发布时间】:2016-03-08 19:21:30
【问题描述】:

我正在使用 Java 进行一些编码,并且正在处理非常大的整数。我正在使用 BigInteger 来获取我的结果,并且我正在寻找某种方法来用逗号分隔我的结果,这样一切看起来都很好而且整洁。下面是一个简单的例子,我希望你们能帮助我

    import java.math.*;

    public static void main(String []args){
       BigInteger integer = BigInteger.valueOf(60000);

       String result = integer.toString();
       System.out.printf("%,8d%n",integer);
    }

我不知道从哪里开始。有什么帮助吗? 它似乎不像在字符串中添加逗号那样简单,因为 BigInteger 不能直接转换为字符串,而是可以成为字符串的一部分。

【问题讨论】:

  • 您说的是“3250780 --> 3,250,780”(例如)?
  • 你能举一些清楚的例子来说明你在寻找什么吗??
  • "3250780 --> 3,250,780" 正是我要找的。​​span>

标签: java biginteger comma


【解决方案1】:

你可以用like

public static void main(String[] args) {
    BigInteger integer = BigInteger.valueOf(60000);

    String result = NumberFormat.getNumberInstance(Locale.US).format(
            integer);
    System.out.println(result);
}

输出:60,000

【讨论】:

  • 就是这样。谢谢!我会在我的代码中使用它。
  • @Shondeslitch,感谢您的指正。
  • @DarthVoider,如果这个答案对你有帮助,请采纳。
【解决方案2】:

在 java7+ 中你不需要任何代码就可以做到这一点,只需写 60,000 甚至像这样使用下划线 60_000

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多