程序如下:

 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 将一个正整数翻译成人民币大写的读法的更多相关文章

  1. 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".

    题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...

  2. js 将数字转换成人民币大写的方法

    //将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...

  3. 算法--java实现将数字转换成人民币大写(迅雷面试题)

    今天去迅雷面试,是个数字转换成人民币的算法题: public class Rmb { /** * 人民币的基本信息和操作 * * @author soyoungboy * @version 1.0 * ...

  4. 将一个浮点数转换成人民币读法字符串(java)

    public class Num2Rmb   {       private String[] hanArr = {"零" , "壹" , "贰&qu ...

  5. java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    首先我们的算法是:例如 输入的是 90 1.找到90的最小公约数(1除外)是 2 2.然后把公约数 2 输出 3.接着用 90 / 2 = 45 (如果这里是素数,就结束,否则继续找最小公约数) 4. ...

  6. java将一个list转换成一个String,中间用分隔符隔开

    List sn=[123,1231,1231,231] sn.toString();//[123,1231,1231,231] sn.join(',').toString();//123,1231,1 ...

  7. Java实现人民币大写精讲

    想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...

  8. Java实现人民币大写代码解析

    想要实现人民币大写,在发票等场景中使用?? 1234.56显示为:壹仟贰佰叁拾肆元伍角陆分,那就往下看看吧! 本程序可以实现 0 到 9999 9999 9999.994 以内的人民币大写转换,精确到 ...

  9. Java将一个字符串的首位改为大写后边改为小写的实现,String

    Java将一个字符串的首位改为大写后边改为小写的实现,String 思路: 获取首字母, charAt(0) substring(0,1) 转成大写 toUpperCase() 转大写hellO=== ...

随机推荐

  1. UmUtils得到友盟的渠道号

    import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm ...

  2. openstack compute service list Unable to establish connection to http://controller:8774/v2.1/os-services: ('Connection aborted.', BadStatusLine("''",))

    8774是nova的端口号,所以我就逐一查看nova的日志文件. tail -f /var/log/nova/nova-conductor.log 2019-06-13 08:24:53.559 44 ...

  3. ElasticSearch——常用命令

    集群相关 --查询集群健康状态 GET _cluster/health --查询所有节点 GET _cat/nodes --查询索引及分片的分布 GET _cat/shards --查询指定索引分片的 ...

  4. Base64加密 解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. 3年磨一剑,我的前端数据 mock 库 http-mock-middleware

    不好意思,离开博客园4年多了,一回来就是为自己打广告,真是害羞啊... http-mock-middleware 是我最近完成的一个前端数据 mock 库.它是我汇总近3年工作经验而诞生的一个工具,使 ...

  6. 一、Node.js安装及环境配置之Windows篇

    一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Node.js:v6.9.2LTS(64位) 二.安装Node.js步骤 1.下载对应你系统的Node.js版本:https:// ...

  7. APP安全_Android渗透环境

    Android渗透 移动APP大多通过WEB API服务的方式与服务端进行交互,这种模式把移动安全和web安全绑在一起.常见的web漏洞在移动APP中也存在,比如SQL注入,文件上传,中间件/serv ...

  8. Mysql安装后在服务里找不到和服务启动不起来的解决方法

    一,在安装完Mysql数据库后,发现在控制面板->管理->服务中找不到Mysql的服务启动 解决方法如下:开启命令行,按照如下步骤即可: 1.进入到mysql的安装包,在bin里执行:my ...

  9. DAY 吐

    今天所学: 一,Linux的文件和目录管理 #1 cd( 变更用户所在目录)直接运行cd会进入root的/root下,后面跟目录名,会进入指定目录下( 后面只能是目录名,不能跟文件名). #2 pwd ...

  10. PYTHON 100days学习笔记004:循环结构

    目录 Day04 - 循环结构 1. 循环结构的应用场景 2.for-in循环 3. while循环 4. 练习 4.1 输入一个数判断是不是素数. 4.2 输入两个正整数,计算最大公约数和最小公倍数 ...