题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
//其实这道题思路不难, 只是条件过程比较多, 花的时间比较多, 可能考试做不完, 所以定为 较难
//首先是处理小数点后面的, 要么多少元整, 要么几角几分, 但是要注意1.00变成字符串可能就是1.0需要添加一个0
//然后是四个一组的处理, 两个循环, 第一次循环四个一次, 会加上进位, (元,万,亿),但是<1就不能加上元
//数字直接用数组调用, 单位 个十百千,个不处理, 10的时候不能加入壹拾,
//然后就是0的处理用一个后面是否为0即可
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//1、中文大写金额数字前应标明“人民币”字样。中文大写金额数字应用
// 壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样填写。
//2、中文大写金额数字到“元”为止的,在“元”之后,应写“整字,如532.00应写成“人民币伍佰叁拾贰元整”。
// 在”角“和”分“后面不写”整字。
//3、阿拉伯数字中间有“0”时,中文大写要写“零”字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个“零”字,
// 如6007.14,应写成“人民币陆仟零柒元壹角肆分“。
//4、10应写作“拾”,100应写作“壹佰”。例如,1010.00应写作“人民币壹仟零拾元整”,110.00应写作“人民币壹佰拾元整”
//5、十万以上的数字接千不用加“零”,例如,30105000.00应写作“人民币叁仟零拾万伍仟元整”
Scanner sc = new Scanner(System.in);
double n = sc.nextDouble();
String []shuzi = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String []jinwei = new String[]{ "元", "万","亿"};
String []sigeyizu = new String[]{ "个","拾","佰","仟"};
String res = "";
//处理小数点后面的
String str = ""+n;//123.00
int xiaoshudian = str.indexOf(".");
for (int i = 0; i < 2 - (str.length()-xiaoshudian-1); i++)
{
str =str+0;
}
if(str.charAt(str.length()-1)=='0' && str.charAt(str.length()-2)=='0')
{
res = res + "整";
}
else
{
char fen = str.charAt(str.length()-1);
char jiao = str.charAt(str.length()-2);
int iFen = fen - '0';
int iJiao = jiao - '0';
if(iFen!=0)
{
res =res+ shuzi[iFen]+ "分" ;
}
if(iJiao!=0)
{
res = shuzi[iJiao] + "角"+res;
}
}
if( n<1 )
{
res = "人民币"+res;
System.out.println(res);
return;
}
//15 1121.15 人民币拾伍万壹仟壹佰贰拾壹 元壹角伍分
//处理小数点后面的
int numJinwei = 0;
boolean afteris0 = false;
for (int i = str.length()-4 ; i >=0 ; i = i-4 )
{
res = jinwei[numJinwei] + res;
numJinwei++;
int danwei = 0;
for (int j = i; j >i-4 && j>=0; j--)
{
char ch = str.charAt(j);
int index = ch-'0';
if( ch =='0' && !afteris0 )
{
if(res.charAt(0) != '元')
{
res = shuzi[0] +res;
}
afteris0 = true;
danwei++;
continue;
}
if(ch =='0' && afteris0)
{
danwei++;
continue;
}
if(ch !='0' && afteris0)
{
afteris0 = false;
}
if(danwei==0)
{
res = shuzi[index] +res;
}
else
{
if(sigeyizu[danwei].equals("拾") && index==1)
{
res = sigeyizu[danwei] +res;
}
else
{
res = shuzi[index] + sigeyizu[danwei] +res;
}
}
danwei++;
}
}
res = "人民币"+res;
System.out.println(res);
}
}

查看7道真题和解析