例如我有一个不确定的金额数,如何把它格式化成200,000,000的形式输出

解决方案 »

  1.   

    import java.text.*;// The 0 symbol shows a digit or 0 if no digit present
        NumberFormat formatter = new DecimalFormat("000000");
        String s = formatter.format(-1234.567);  // -001235
        // notice that the number was rounded up
        
        // The # symbol shows a digit or nothing if no digit present
        formatter = new DecimalFormat("##");
        s = formatter.format(-1234.567);         // -1235
        s = formatter.format(0);                 // 0
        formatter = new DecimalFormat("##00");
        s = formatter.format(0);                 // 00
        
        
        // The . symbol indicates the decimal point
        formatter = new DecimalFormat(".00");
        s = formatter.format(-.567);             // -.57
        formatter = new DecimalFormat("0.00");
        s = formatter.format(-.567);             // -0.57
        formatter = new DecimalFormat("#.#");
        s = formatter.format(-1234.567);         // -1234.6
        formatter = new DecimalFormat("#.######");
        s = formatter.format(-1234.567);         // -1234.567
        formatter = new DecimalFormat(".######");
        s = formatter.format(-1234.567);         // -1234.567
        formatter = new DecimalFormat("#.000000");
        s = formatter.format(-1234.567);         // -1234.567000
        
        
        // The , symbol is used to group numbers
        formatter = new DecimalFormat("#,###,###");
        s = formatter.format(-1234.567);         // -1,235
        s = formatter.format(-1234567.890);      // -1,234,568
        
        // The ; symbol is used to specify an alternate pattern for negative values
        formatter = new DecimalFormat("#;(#)");
        s = formatter.format(-1234.567);         // (1235)
        
        // The ' symbol is used to quote literal symbols
        formatter = new DecimalFormat("'#'#");
        s = formatter.format(-1234.567);         // -#1235
        formatter = new DecimalFormat("'abc'#");
        s = formatter.format(-1234.567);         // -abc1235
      

  2.   

    import java.text.*;public class FormatNumber
    {
    public static void main(String str[])
    {
    NumberFormat nf = NumberFormat.getInstance();
    String str1 = nf.format(200000000l);
    System.out.println(str1);
    }
    }
    //还可以设置为其他的输出方式,自己查看API喽!
      

  3.   

    NumberFormat formatter = new DecimalFormat("#,###,###");
    String s = formatter.format(1234567);
      

  4.   

    NumberFormat 和 DecimalFormat 的作用在于一部分控制用于格式化
      

  5.   

    int x=200,000,000;
    NumberFormat formatter=NumberFormat.getNumberInstance();
    System.out.println(formatter.format(x));