程序如下:

 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. 深度学习之加载VGG19模型获取特征图

    1.加载VGG19获取图片特征图 # coding = utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplo ...

  2. Vue.js父子组件如何传值 通俗易懂

    父子组件传值原理图 一般页面的视图App.vue应为这样 一.父组件向子组件传值 1.创建子组件,在src/components/文件夹下新建一个Child.vue 2.Child.vue的中创建pr ...

  3. Java相关框架概念以及思想

    1.什么是IoC Ioc—Inversion of Control,即“控制反转”,是一种思想, 一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合.更优良的程序. 高内聚低耦合的设计能够让构 ...

  4. 阻塞IO和非阻塞IO的区别

    转载地址: http://blog.sina.com.cn/s/blog_a46817ff0101g0gv.html http://blog.csdn.net/nodeathphoenix/artic ...

  5. GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...

  6. spring 配置参数从配置文件中加载到PropertiesFactoryBean 和配置参数从数据库加载到PropertiesFactoryBean 的实现,及项目中的相关应用

    1.加载.properties文件中的配置参数加载到PropertiesFactoryBean容器中 <bean id="configProperties" class=&q ...

  7. ETH挖矿

    转载声明:http://www.120btc.com/baike/coin/1021.html (仅为了方便自己及原文章被删除) 最近以太坊大涨市值成为仅次比特币的第二大数字货币,那么以太坊(ETH) ...

  8. 冒泡排序到demo

    package com.lmy.demoSort; /** * 冒泡排序demo * @author Yubaba * */ public class BubbleSort { public stat ...

  9. hive中case命令

  10. POJ3450最长公共子串【kmp】

    题目链接:http://poj.org/problem?id=3450 题目大意:给定n个长度不超过200的字符串,n < 4000.求这些字符串的最长公共子串,若没有,则输出 “IDENTIT ...