java 将一个正整数翻译成人民币大写的读法
程序如下:
import java.lang.StringBuffer;
/**
给定一个浮点数,将其装换成人民币大写的读法
88.5:捌十捌元零伍角
*/
public class Num2Rmb
{
private String[] hanArr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; //将一个浮点数分解成整数部分和小数部分,小数部分保留两位小数
private String[] divide(double num)
{
long zheng=(long)num;
long xiao=Math.round((num-zheng)*100);
return new String[]{zheng+"",String.valueOf(xiao)};
} private String toHanStr(String numStr)
{
String str="";
//先转换正数
int numLen=numStr.length();
if(numLen>9)
{
System.out.println("输入的浮点数的正数部分的长度不能超过9,最大为9.");
return "";
}
for(int i=0;i<numLen;i++)
{
int number= numStr.charAt(i)-48;
str+=hanArr[number];
}
//给正数部分添加单位
/**
1 2 3 4 5 6 7 8 9
1亿2千3百4十5万6千7百8十9
*/
String[] unit=new String[]{"亿","千","百","十","万"};
String newStr="";
switch(numLen)
{
case 9:
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[0];
if(i==1)
newStr+=ch+unit[1];
if(i==2)
newStr+=ch+unit[2];
if(i==3)
newStr+=ch+unit[3];
if(i==4)
newStr+=ch+unit[4];
if(i==5)
newStr+=ch+unit[1];
if(i==6)
newStr+=ch+unit[2];
if(i==7)
newStr+=ch+unit[3];
if(i==8)
newStr+=ch;
}
break;
case 8://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[1];
if(i==1)
newStr+=ch+unit[2];
if(i==2)
newStr+=ch+unit[3];
if(i==3)
newStr+=ch+unit[4];
if(i==4)
newStr+=ch+unit[1];
if(i==5)
newStr+=ch+unit[2];
if(i==6)
newStr+=ch+unit[4];
if(i==7)
newStr+=ch;
}
break;
case 7://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[2];
if(i==1)
newStr+=ch+unit[3];
if(i==2)
newStr+=ch+unit[4];
if(i==3)
newStr+=ch+unit[1];
if(i==4)
newStr+=ch+unit[2];
if(i==5)
newStr+=ch+unit[3];
if(i==6)
newStr+=ch;
}
break;
case 6://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[3];
if(i==1)
newStr+=ch+unit[4];
if(i==2)
newStr+=ch+unit[1];
if(i==3)
newStr+=ch+unit[2];
if(i==4)
newStr+=ch+unit[4];
if(i==5)
newStr+=ch;
}
break;
case 5://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[4];
if(i==1)
newStr+=ch+unit[1];
if(i==2)
newStr+=ch+unit[2];
if(i==3)
newStr+=ch+unit[3];
if(i==4)
newStr+=ch;
}
break;
case 4://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[1];
if(i==1)
newStr+=ch+unit[2];
if(i==2)
newStr+=ch+unit[3];
if(i==3)
newStr+=ch;
}
break;
case 3://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[2];
if(i==1)
newStr+=ch+unit[3];
if(i==2)
newStr+=ch;
}
break;
case 2://
for(int i=0;i<numLen;i++)
{
//取字符
char ch=str.charAt(i);
if(i==0)
newStr+=ch+unit[3];
if(i==1)
newStr+=ch;
}
break;
case 1:
newStr+=str;
break;
default:
break;
}
newStr+="元";
String endStr="";
StringBuffer sb=new StringBuffer(newStr);
//处理转换后的字符串里面多个零的情况,寄将零后面的单位去掉
for(int j=0;j<newStr.length();j++)
{
char ch=newStr.charAt(j);
if(ch=='零')
{
sb.replace(j+1,j+2,"*");
} }
//if(index>0)//
endStr=sb.toString().replace("*",""); StringBuffer sb0=new StringBuffer(endStr);
int dou=sb0.toString().indexOf("零零");
if(dou>0)
{
sb0.replace(dou,dou+1,"");
}
return sb0.toString(); } public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("abbcdf");
int index0=sb.toString().indexOf("bb");
System.out.println(index0);
sb.replace(index0,index0+2,"m");
System.out.println(sb.toString());
String s="abcd";
int index=s.indexOf("f");
System.out.println(index);
Num2Rmb nr=new Num2Rmb();
String[] numStr= nr.divide(100345678.25);
String str= nr.toHanStr(numStr[0]);
System.out.println(str);
}
} 运行效果如下:

java 将一个正整数翻译成人民币大写的读法的更多相关文章
- 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".
题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...
- js 将数字转换成人民币大写的方法
//将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...
- 算法--java实现将数字转换成人民币大写(迅雷面试题)
今天去迅雷面试,是个数字转换成人民币的算法题: public class Rmb { /** * 人民币的基本信息和操作 * * @author soyoungboy * @version 1.0 * ...
- 将一个浮点数转换成人民币读法字符串(java)
public class Num2Rmb { private String[] hanArr = {"零" , "壹" , "贰&qu ...
- java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...
- java将一个list转换成一个String,中间用分隔符隔开
List sn=[123,1231,1231,231] sn.toString();//[123,1231,1231,231] sn.join(',').toString();//123,1231,1 ...
- Java实现人民币大写精讲
想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...
- Java实现人民币大写代码解析
想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...
- Java将一个字符串的首位改为大写后边改为小写的实现,String
Java将一个字符串的首位改为大写后边改为小写的实现,String 思路: 获取首字母, charAt(0) substring(0,1) 转成大写 toUpperCase() 转大写hellO=== ...
随机推荐
- Tomcat安装应用部署及配置文件解读
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat和Nginx,APa ...
- 一个伪静态与404重定向例子(房产网),.htaccess文件内容
ErrorDocument 404 /404.phpRewriteEngine OnRewriteBase /RewriteRule ^(.*)\.(asp|aspx|asa|asax|dll|jsp ...
- R语言与概率统计(一) 描述性统计分析
#查看已安装的包,查看已载入的包,查看包的介绍 ########例题3.1 #向量的输入方法 w<-c(75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 6 ...
- 直连路由onlink
根据路由器学习路由信息.生成并维护路由表的方法包括直连路由(Direct).静态路由(Static)和动态路由(Dynamic).直连路由:路由器接口所连接的子网的路由方式称为直连路由:非直连路由:通 ...
- playbook文件内容解释
[root@node1 playbook]# cat nginx.yml - hosts: test \\主机组,要和nginx.yml在同一个目录下 remote_user: root \\远端执行 ...
- DevOps - CI&CD
1 - CI与CD的联系与区别 持续集成(Continuous Integration).持续交付(Continuous Delivery)和持续部署(Continuous Deployment)的过 ...
- Spark读取HDFS中的Zip文件
1. 任务背景 近日有个项目任务,要求读取压缩在Zip中的百科HTML文件,经分析发现,提供的Zip文件有如下特点(=>指代对应解决方案): (1) 压缩为分卷文件 => 只需将解压缩在同 ...
- 想了解Java后端学习路线?你只需要这一张图!
前言 学习路线图往往是学习一样技术的入门指南.网上搜到的Java学习路线图也是一抓一大把. 今天我只选一张图,仅此一图,足以包罗Java后端技术的知识点.所谓不求最好,但求最全,学习Java后端的同学 ...
- docker 启动镜像报 WARNING: IPv4 forwarding is disabled. Networking will not work.
centos7 解决办法: # vi /etc/sysctl.conf 添加如下代码: net.ipv4.ip_forward=1 重启network服务 # systemctl restar ...
- Cocos2d-x_初探_第一次配置与HelloWorld
此前多久,忘了,反正就是打通关泡泡龙以后.YY君向我推荐了这个(如题).一查资料,诶.还挺有意思的,那我就去下一个玩玩吧. 资料下载清单: 1.Android-ndk 2.Android-sdk 3. ...