1001

1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where 106​​a,b106​​. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

package _1001;

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = a+b;
            System.out.format("%,d",c);
        }
    }
}

 

  MessageFormat、DateFormat、NumberFormat是Format三个常用的子类。

  还可以通过NumberFormat类来对数字进行格式化输出。

  DecimalFormat也是Format的一个子类,格式化数字时比NumberFormat更加方便。

符号 

位置 本地化 含义
0 数字 阿拉伯数字
# 数字 阿拉伯数字,不存在则显示为0
. 数字 小数分隔符或货币小数分隔符
- 数字 减号
, 数字 分组分隔符
E 数字 分割隔科学计数法中的尾数和指数。在前缀后缀中无需加引号
; 子模式边界 分隔正数和负数子模式
% 前缀或后缀 乘以100并显示为百分数
\u2030 前缀或后缀 乘以1000并显示为千分数
' 前缀或后缀 用于在前缀或后缀中为特殊字符加引号
       
package _1001;

import java.text.DecimalFormat;

public class Test {

    public static void main(String[] args){
        double a = 10000.1111;
        formattest("###,###.###",a);
        formattest("000,000.000",a);
        formattest("###,###.###¥",a);
        formattest("000,000.000¥",a);
        formattest("##.###%",a);
        formattest("00.###%",a);
        formattest("###.###\u2030",a);
    }

    public static void formattest(String pattern,double value){
        DecimalFormat df = new DecimalFormat(pattern);
        String str = df.format(value);
        System.out.println(pattern+" : "+str);
    }
}

 

###,###.### : 10,000.111
000,000.000 : 010,000.111
###,###.###¥ : 10,000.111000,000.000¥ : 010,000.111¥
##.###% : 1000011.11%
00.###% : 1000011.11%
###.###‰ : 10000111.1‰
Result

 

 

 
posted @ 2018-09-25 20:01  挤成肉夹馍  阅读(280)  评论(0)    收藏  举报