题目描述
考试题目和要点:
1、中文大写金额数字前应标明“人民币”字样。中文大写金额数字应用壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样填写。(30分)
2、中文大写金额数字到“元”为止的,在“元”之后,应写“整字,如¥ 532.00应写成“人民币伍佰叁拾贰元整”。在”角“和”分“后面不写”整字。(30分)
3、阿拉伯数字中间有“0”时,中文大写要写“零”字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个“零”字,如¥6007.14,应写成“人民币陆仟零柒元壹角肆分“。(
输入描述:
输入一个double数
输出描述:
输出人民币格式
输入例子:
151121.15
输出例子:
人民币拾伍万壹仟壹佰贰拾壹元壹角伍分
1 import java.util.*; 2 3 public class Main{ 4 public static String[] num1 = {" ","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; 5 public static void main(String[] agrs){ 6 Scanner sc = new Scanner(System.in); 7 while(sc.hasNext()){ 8 double n = sc.nextDouble(); 9 long num = (long)(100*(n+0.001));//0.29 不是精确的0.29 可能是0.28999999999 直接乘以100 可能得到28 +0.001 10 //System.out.println(num); 11 System.out.println(parse(num)); 12 } 13 } 14 public static String parse(long num){ 15 if(num < 0) 16 return "error"; 17 StringBuilder sb = new StringBuilder("人民币"); 18 //亿部分 19 long yi = num / 10000000000l; //Main45.java:20: 错误: 过大的整数: 10000000000 long yi = num / 10000000000; 加l 20 if(yi != 0){ 21 sb.append(thranslate(yi) + "亿"); 22 } 23 //万部分 24 num = num % 10000000000l; 25 long wan = num / 1000000; 26 if(wan != 0){ 27 sb.append(thranslate(wan) + "万"); 28 } 29 //千部分 30 num = num % 1000000; 31 long qian = num / 100000; 32 if(qian != 0){ 33 sb.append(thranslate(qian) + "千"); 34 } 35 //百部分 36 num = num % 100000; 37 long bai = num / 10000; 38 if(bai != 0){ 39 sb.append(thranslate(bai)+ "佰"); 40 } 41 num = num % 10000; 42 long shi = num / 1000; 43 if(shi != 0){ 44 if(shi==1) 45 sb.append("拾");//注意:13只说拾叁不说壹拾叁 46 else 47 sb.append(num1[(int)shi] + "拾"); 48 } 49 num = num % 1000; 50 long yuan = num / 100; 51 if(yuan != 0){ 52 sb.append(thranslate(yuan) + "元"); 53 } 54 num = num % 100; 55 long jiao = num / 10; 56 if(jiao != 0){ 57 sb.append(thranslate(jiao) + "角"); 58 } 59 num = num % 10; 60 long fen = num; 61 if(num % 10 != 0){ 62 sb.append(thranslate(fen) + "分"); 63 } 64 if(jiao == 0&&num % 10 == 0) 65 sb.append("整"); 66 return sb.toString().trim(); 67 } 68 public static String thranslate(long num){ 69 StringBuilder sb = new StringBuilder(); 70 long q = num / 1000; 71 if(q != 0){ 72 sb.append(num1[(int)q] + "千"); 73 } 74 num = num % 1000; 75 long h = num / 100; 76 if(h != 0){ 77 sb.append(num1[(int)h] + "佰"); 78 } 79 num = num % 100; 80 long s = num / 10; 81 if(s != 0){ 82 if(s==1) 83 sb.append("拾"); 84 else 85 sb.append(num1[(int)s] + "拾"); 86 } 87 num = num % 10; 88 if(num != 0){ 89 sb.append(num1[(int)num]); 90 } 91 return sb.toString(); 92 } 93 94 }
对零的处理还有待思考改善。。花了2个小时才做成这样
时间: 2024-10-02 11:37:51