华为-人民币转换
(java实现)
题目描述:
考试题目和要点:
1、中文大写金额数字前应标明“人民币”字样。中文大写金额数字应用壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样填写。
2、中文大写金额数字到“元”为止的,在“元”之后,应写“整字,如532.00应写成“人民币伍佰叁拾贰元整”。在”角“和”分“后面不写”整字。
3、阿拉伯数字中间有“0”时,中文大写要写“零”字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个“零”字,如6007.14,应写成“人民币陆仟零柒元壹角肆分“。
4、10应写作“拾”,100应写作“壹佰”。例如,1010.00应写作“人民币壹仟零拾元整”,110.00应写作“人民币壹佰拾元整”
5、十万以上的数字接千不用加“零”,例如,30105000.00应写作“人民币叁仟零拾万伍仟元整”
本题含有多组样例输入。
输入描述:
输入一个double数
输出描述:
输出人民币格式
示例1:
输入
151121.15 10012.02
输出
人民币拾伍万壹仟壹佰贰拾壹元壹角伍分 人民币壹万零拾贰元贰分
问题分析:
按4位数一组来分析;
同时要考虑特殊情况,如“整”、十万以上接千不用加“零”等。
相关知识:
略
算法实现:
略
参考代码:
import java.util.*;
public class Main {
public static String[] num0 = { "","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while (input.hasNext())
{
String line = input.nextLine();
String[] str = line.split("\\.");
long num = Long.parseLong(str[0]);
int point = Integer.parseInt(str[1]);
String res = "人民币";
//处理整数部分
long bil = num/100000000;
if (bil > 0)
{
res = res + turnToRMB(bil) + "亿";
}
num %= 100000000;//num /= 100000000;
long mil = num/10000;
if (mil > 10)
{
res = res + turnToRMB(mil) + "万";
}else if (mil>0)
{
res = res + turnToRMB(mil) + "万零";
}
num %= 10000;//num /= 10000;
res = res + turnToRMB(num);
if (bil>0 || mil>0 || num>0)
{
res += "元";
}
//处理小数部分
if (0 == point)
{
res += "整";
}else
{
int tmp = point/10;
if (tmp > 0)
{
res = res + num0[tmp] + "角";
}
tmp = point%10;
if (tmp > 0)
{
res = res + num0[tmp] + "分";
}
}
System.out.println(res);
}
}
public static String turnToRMB(long number)
{
String res = "";
int num = (int)number;
int k = num/1000;
if (k > 0)
{
res = res + num0[k] + "仟";
}
num %= 1000;
int h = num/100;
if (h > 0)
{
res = res + num0[h] + "佰";
}
num %= 100;
int t = num/10;
if (k>0 && (0==h||0==t) && num>0)
{
res += "零";
}
num %= 10;
if (t > 1)
{
res = res + num0[t] + "拾";
}else if (1 == t)
{
res = res + "拾";
}
if (num > 0)
{
res = res + num0[num];
}
return res;
}
}
