金額轉大寫(可處理千萬億,精確到分)
阿新 • • 發佈:2019-02-02
package org.GE.action;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-4-12
* Time: 上午11:18
* To change this template use File | Settings | File Templates.
*/
public class Test1{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("請輸入需要轉換的數字:");
String i = sc.nextLine();
System.out.println(digitUppercase(i));
}
}
/**
* 處理的最大數字達千萬億位 精確到分
* @return
*/
public static String digitUppercase(String num) throws Exception{
String fraction[] = {"角", "分"};
String digit[] = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };
/**
* 仟 佰 拾 ' '
' ' $4 $3 $2 $1
萬 $8 $7 $6 $5
億 $12 $11 $10 $9
*/
String unit1[] = {"", "拾", "佰", "仟"};//把錢數分成段,每四個一段,實際上得到的是一個二維陣列
String unit2[] = {"元", "萬", "億","萬億"}; //把錢數分成段,每四個一段,實際上得到的是一個二維陣列
BigDecimal bigDecimal = new BigDecimal(num);
bigDecimal=bigDecimal.multiply(new BigDecimal(100));
// Double bigDecimal = new Double(name*100); 存在精度問題 eg:145296.8
String strVal = String.valueOf(bigDecimal.toBigInteger());
String head = strVal.substring(0,strVal.length()-2); //整數部分
String end = strVal.substring(strVal.length()-2); //小數部分
String endMoney="";
String headMoney = "";
if("00".equals(end)){
endMoney = "整";
}else{
if(!end.substring(0,1).equals("0")){
endMoney+=digit[Integer.valueOf(end.substring(0,1))]+"角";
}else if(end.substring(0,1).equals("0") && !end.substring(1,2).equals("0")){
endMoney+= "零";
}
if(!end.substring(1,2).equals("0")){
endMoney+=digit[Integer.valueOf(end.substring(1,2))]+"分";
}
}
char[] chars = head.toCharArray();
Map<String,Boolean> map = new HashMap<String,Boolean>();//段位置是否已出現zero
boolean zeroKeepFlag = false;//0連續出現標誌
int vidxtemp = 0;
for(int i=0;i<chars.length;i++){
int idx = (chars.length-1-i)%4;//段內位置 unit1
int vidx = (chars.length-1-i)/4;//段位置 unit2
String s = digit[Integer.valueOf(String.valueOf(chars[i]))];
if(!"零".equals(s)){
headMoney += s +unit1[idx]+unit2[vidx];
zeroKeepFlag = false;
}else if(i==chars.length-1 || map.get("zero"+vidx)!=null){
headMoney += "" ;
}else{
headMoney += s;
zeroKeepFlag = true;
map.put("zero"+vidx,true);//該段位已經出現0;
}
if(vidxtemp!=vidx || i==chars.length-1){
headMoney = headMoney.replaceAll(unit2[vidx],"");
headMoney+=unit2[vidx];
}
if(zeroKeepFlag && (chars.length-1-i)%4==0){
headMoney = headMoney.replaceAll("零","");
}
}
return headMoney+endMoney;
}
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-4-12
* Time: 上午11:18
* To change this template use File | Settings | File Templates.
*/
public class Test1{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("請輸入需要轉換的數字:");
String i = sc.nextLine();
System.out.println(digitUppercase(i));
}
}
/**
* 處理的最大數字達千萬億位 精確到分
* @return
*/
public static String digitUppercase(String num) throws Exception{
String fraction[] = {"角", "分"};
String digit[] = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };
/**
* 仟 佰 拾 ' '
' ' $4 $3 $2 $1
萬 $8 $7 $6 $5
億 $12 $11 $10 $9
*/
String unit1[] = {"", "拾", "佰", "仟"};//把錢數分成段,每四個一段,實際上得到的是一個二維陣列
String unit2[] = {"元", "萬", "億","萬億"}; //把錢數分成段,每四個一段,實際上得到的是一個二維陣列
BigDecimal bigDecimal = new BigDecimal(num);
bigDecimal=bigDecimal.multiply(new BigDecimal(100));
// Double bigDecimal = new Double(name*100); 存在精度問題 eg:145296.8
String strVal = String.valueOf(bigDecimal.toBigInteger());
String head = strVal.substring(0,strVal.length()-2); //整數部分
String end = strVal.substring(strVal.length()-2); //小數部分
String endMoney="";
String headMoney = "";
if("00".equals(end)){
endMoney = "整";
}else{
if(!end.substring(0,1).equals("0")){
endMoney+=digit[Integer.valueOf(end.substring(0,1))]+"角";
}else if(end.substring(0,1).equals("0") && !end.substring(1,2).equals("0")){
endMoney+= "零";
}
if(!end.substring(1,2).equals("0")){
endMoney+=digit[Integer.valueOf(end.substring(1,2))]+"分";
}
}
char[] chars = head.toCharArray();
Map<String,Boolean> map = new HashMap<String,Boolean>();//段位置是否已出現zero
boolean zeroKeepFlag = false;//0連續出現標誌
int vidxtemp = 0;
for(int i=0;i<chars.length;i++){
int idx = (chars.length-1-i)%4;//段內位置 unit1
int vidx = (chars.length-1-i)/4;//段位置 unit2
String s = digit[Integer.valueOf(String.valueOf(chars[i]))];
if(!"零".equals(s)){
headMoney += s +unit1[idx]+unit2[vidx];
zeroKeepFlag = false;
}else if(i==chars.length-1 || map.get("zero"+vidx)!=null){
headMoney += "" ;
}else{
headMoney += s;
zeroKeepFlag = true;
map.put("zero"+vidx,true);//該段位已經出現0;
}
if(vidxtemp!=vidx || i==chars.length-1){
headMoney = headMoney.replaceAll(unit2[vidx],"");
headMoney+=unit2[vidx];
}
if(zeroKeepFlag && (chars.length-1-i)%4==0){
headMoney = headMoney.replaceAll("零","");
}
}
return headMoney+endMoney;
}
}
轉自:http://605113068.iteye.com/blog/2049163