zoukankan      html  css  js  c++  java
  • [java] 将整数在千分位或万分位以逗号分隔表示

    简单使用DecimalFormat的功能就能做到了,代码如下:

    package com.testEmp;
    
    import java.text.DecimalFormat;
    
    public class NumberFormat {
        public static void main(String[] args) {
            long[] arr= {1,2345,676767,664564545,4324324324432432443L};
            
            for(long num:arr) {
                System.out.println(toEastNumFormat(num));
            }
        }
        
        // 将整数在千分位以逗号分隔表示
        public static String toWestNumFormat(long number) {
            DecimalFormat df = new DecimalFormat("#,###");
            return df.format(number);
        }
        
        // 将整数在万分位以逗号分隔表示
        public static String toEastNumFormat(long number) {
            DecimalFormat df = new DecimalFormat("#,####");
            return df.format(number);
        }
    }

    运行示例:

    1
    2345
    67,6767
    6,6456,4545
    432,4324,3244,3243,2443
    1
    2,345
    676,767
    664,564,545
    4,324,324,324,432,432,443

    --END 2019年10月5日07:34:06--

  • 相关阅读:
    CSU 1122
    CSU 1256
    CSU 1240
    HDU 1874
    CSU 1004
    Problem F CodeForces 16E
    Problem E CodeForces 237C
    Problem C FZU 1901
    12-30
    2016-12-29
  • 原文地址:https://www.cnblogs.com/heyang78/p/11623878.html
Copyright © 2011-2022 走看看