自己今天写了一个疯狂java上demo——double转人民币大写<pre name="code" class="java">
import java.util.Scanner;
public class num2Rmb {
String [] hanrrr = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String [] unitrrr = {"拾","佰","仟"} ;
double num ;
public num2Rmb (double num) {
this.num = num ;
}
public String []divide(){
long zheng = (long)num ;
long xiao = Math.round((num - zheng) *100) ;
return new String[]{zheng+"",String.valueOf(xiao)} ;
}
public String getZheng(String numStr) {
boolean flag = true ;
String result = "" ;
int index = 0;
//判断第一位是不是0;
while(numStr.charAt(index)=='0'){
index++ ;
}
int numLength = numStr.length() ;
for (int i = index; i < numStr.length()-1; i++) {
int num = numStr.charAt(i) -48;
int pos = numLength - i -2 ;
int tempUnit = pos % 4 ;
if (num!= 0 ) {
flag =false ;
if (tempUnit !=3) {
result +=hanrrr[num]+unitrrr[tempUnit] ;
}else {
result +=hanrrr[num] ;
if(pos / 4 !=1){
result += "万" ;
}else {
result +="亿" ;
}
}
}else if(tempUnit !=3){
if (flag==false) {
result += "零";
}
}else {
if(pos / 4 !=2){
result += "万" ;
}else {
result +="亿" ;
}
}
}
int last = numStr.charAt(numLength - 1)-48 ;
return result+hanrrr[last];
}
public String getXiao(String numStr){
if (numStr.length() == 0) {
return "" ;
}
if (numStr.length() ==1) {
numStr += 0 ;
}
int frist = numStr.charAt(0) - 48;
int second = numStr.charAt(1) - 48;
if (frist != 0 && second != 0) {
return hanrrr[frist]+"角"+hanrrr[second]+"分" ;
}else if (frist != 0 ) {
return hanrrr[frist]+"角" ;
}else if(second != 0) {
return hanrrr[second]+"分" ;
}else {
return "" ;
}
}
public String getEntity(){
String [] parts = divide();
String zheng = getZheng(parts[0]);
String xiao = getXiao(parts[1]) ;
return zheng+xiao ;
}
public static void main(String[] rgs) {
System.out.print("请输入您的金额:");
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble() ;
num2Rmb temp = new num2Rmb(num);
sc.close();
System.out.println();
System.out.print("输出:");
System.out.println(temp.getEntity());
}
}
浮点数转换成人民币读法字符串
最新推荐文章于 2021-06-04 16:16:00 发布