本文目錄一覽:
java 金額轉換
private static String chainNum[] = { “零”, “壹”, “貳”, “叄”, “肆”, “伍”, “陸”,
“柒”, “捌”, “玖”, “拾”, “元”, “角”, “分”, “佰”, “仟”, “萬”, “億” };
private static String num2chainNum(String temp) {
String tmp = temp.toString();
String chainNum_ = “”;
String _chainNum = “”;
String[] p = tmp.split(“\\.”);
String decimalSection = null;
String num = p[0];
if (p.length 1) {
decimalSection = p[1];
}
if (decimalSection != null) {
decimalSection = fill0(decimalSection, 2);
if (decimalSection.charAt(0) != ‘0’) {
_chainNum = _chainNum
+ chainNum[Integer.valueOf(Character.valueOf(
decimalSection.charAt(0)).toString())] + “角”;
}
if (decimalSection.charAt(1) != ‘0’) {
_chainNum = _chainNum
+ chainNum[Integer.valueOf(Character.valueOf(
decimalSection.charAt(1)).toString())] + “分”;
}
}
int start = 0;
if (num.length() 8) {
start = 0;
if (num.length() 12) {
start = num.length() – 12;
}
String yi = num.substring(start, num.length() – 8);
chainNum_ = chainNum_ + caseChain(yi) + “億”;
}
if (num.length() 4) {
start = 0;
if (num.length() 8) {
start = num.length() – 8;
}
String wan = num.substring(start, num.length() – 4);
chainNum_ = chainNum_ + caseChain(wan) + “萬”;
}
if (num.length() 0) {
start = 0;
if (num.length() 4) {
start = num.length() – 4;
}
String wu = num.substring(start, num.length());
chainNum_ = chainNum_ + caseChain(wu) + “元”;
}
return chainNum_ + _chainNum + “整”;
}
// 只能傳4位數
private static String caseChain(String tmp) {
tmp = fill0(tmp, 4);
String resultValue = “”;
byte status[] = new byte[5];
for (int i = 0; i status.length; i++) {
status[i] = 0;
}
if (‘0’ == tmp.charAt(0)) {
} else {
status[4] = -1;
resultValue = resultValue
+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(0))
.toString())] + “仟”;
}
if (tmp.charAt(1) == ‘0’) {
if (status[4] == 0) {
} else {
if (tmp.charAt(2) != ‘0’ || tmp.charAt(3) != ‘0’)
resultValue = resultValue + “零”;
}
} else {
status[3] = -1;
resultValue = resultValue
+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(1))
.toString())] + “佰”;
}
if (tmp.charAt(2) == ‘0’) {
status[2] = 0;
if ((status[4] == 0 status[3] == 0)
|| (status[4] != 0 status[3] == 0)) {
} else {
if (tmp.charAt(2) != ‘0’ || tmp.charAt(3) != ‘0’)
resultValue = resultValue + “零”;
}
} else {
resultValue = resultValue
+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(2))
.toString())] + “拾”;
}
if (tmp.charAt(3) != ‘0’) {
resultValue = resultValue
+ chainNum[Integer.valueOf(Character.valueOf(tmp.charAt(3))
.toString())];
}
return resultValue;
}
private static String fill0(String tmp, int median) {
int len = tmp.length();
if (len median) {
for (int i = 0; i median – len; i++) {
tmp = “0” + tmp;
}
}
return tmp;
}
將上面代碼放到class裡面 然後調用
num2chainNum(“11231.120”)
JAVA編程 金額轉換
/**
金額轉換,阿拉伯數字的金額轉換成中國傳統的形式如:
(¥1011)-(一千零一拾一元 整)輸出。
*/
import java.io.*;
import java.lang.String;
public class Money{
public static void main(String[] args)throws Exception{
String str=null;
System.out.println(“請輸入您的金額¥:”);
flag:
while(true){
try{BufferedReader in=
new BufferedReader(new InputStreamReader(System.in));
str=in.readLine();
}catch(IOException e){}
for(int i=0;istr.length();i++){
if(str.charAt(i)57||str.charAt(i)48){
System.out.println(“您輸入的金額有誤!請重新輸入”);
continue flag;
}
}
break;
}
char[] ch=str.toCharArray();
for(int i=0;ich.length;i++){
switch(ch[i]){
case ‘0’:{ ch[i]=’零’; break;}
case ‘1’:{ ch[i]=’壹’; break;}
case ‘2’:{ ch[i]=’貳’; break;}
case ‘3’:{ ch[i]=’叄’; break;}
case ‘4’:{ ch[i]=’肆’; break;}
case ‘5’:{ ch[i]=’伍’; break;}
case ‘6’:{ ch[i]=’陸’; break;}
case ‘7’:{ ch[i]=’柒’; break;}
case ‘8’:{ ch[i]=’捌’; break;}
case ‘9’:{ ch[i]=’玖’; break;}
default: ch[i]=’f’;
}
}
int i=0;
switch(ch.length){
case 0:
case 1: {System.out.println(ch[i]+”元整”);}
case 2: {System.out.println(ch[i]+”十”+ch[i+1]+”元整”);}
case 3: {System.out.println(ch[i]+”百”+ch[i+1]+”十”+ch[i+2]+”元整”);}
case 4: {System.out.println(ch[i]+”千”+ch[i+1]+”百”+ch[i+2]+”十”
+ch[i+3]+”元整”); break;}
case 5: {System.out.println(ch[i]+”萬”+ch[i+1]+”千”+ch[i+2]+”百”
+ch[i+3]+”十”+ch[i+4]+”元整”); break;}
case 6: {System.out.println(ch[i]+”十”+ch[i+1]+”萬”+ch[i+2]+”千”
+ch[i+3]+”百”+ch[i+4]+”十”+ch[i+5]+”元整”); break;}
case 7: {System.out.println(ch[i]+”百”+ch[i+1]+”十”+ch[i+2]+”萬”
+ch[i+3]+”千”+ch[i+4]+”百”+ch[i+5]+”十”+ch[i+6]+”元整”); break;}
case 8: {System.out.println(ch[i]+”千”+ch[i+1]+”百”+ch[i+2]+”十”
+ch[i+3]+”萬”+ch[i+4]+”千”+ch[i+5]+”百”+ch[i+6]+”十”+ch[i+7]+”元整”); break;}
case 9: {System.out.println(ch[i]+”億”+ch[i+1]+”千”+ch[i+2]+”百”
+ch[i+3]+”十”+ch[i+4]+”萬”+ch[i+5]+”千”+ch[i+6]+”百”+ch[i+7]+”十”
+ch[i+8]+”元整”); break;}
case 10: {System.out.println(ch[i]+”十”+ch[i+1]+”億”+ch[i+2]+”千”
+ch[i+3]+”百”+ch[i+4]+”十”+ch[i+5]+”萬”+ch[i+6]+”千”+ch[i+7]+”百”+ch[i+8]+”十”
+ch[i+9]+”元整”); break;}
default: System.out.println(“錯誤”);
}
}
}
用java編譯金額的中文大寫轉換。
/**
* 金額小數轉換成中文大寫金額
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { “萬”, “千”, “佰”, “拾”, “億”, “千”, “佰”,
“拾”, “萬”, “千”, “佰”, “拾”, “元”, “角”, “分” };
private static final String NUM[] = { “零”, “壹”, “貳”, “叄”, “肆”, “伍”, “陸”,
“柒”, “捌”, “玖” };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 將金額小數轉換成中文大寫金額
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money 0 || money MAX_VALUE)
return “參數非法!”;
long money1 = Math.round(money * 100); // 四捨五入到分
if (money1 == 0)
return “零元整”;
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用於選擇金額數值
int unitIndex = UNIT.length – strMoney.length(); // unitIndex用於選擇金額單位
boolean isZero = false; // 用於判斷當前為是否為零
String result = “”;
for (; numIndex strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == ‘0’) {
isZero = true;
if (UNIT[unitIndex] == “億” || UNIT[unitIndex] == “萬”
|| UNIT[unitIndex] == “元”) { // 如果當前位是億、萬、元,且數值為零
result = result + UNIT[unitIndex]; //補單位億、萬、元
isZero = false;
}
}else {
if (isZero) {
result = result + “零”;
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分結尾就加”整”字
if (!result.endsWith(“角”)!result.endsWith(“分”)) {
result = result + “整”;
}
//例如沒有這行代碼,數值”400000001101.2″,輸出就是”肆千億萬壹千壹佰零壹元貳角”
result = result.replaceAll(“億萬”, “億”);
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble(“40330701101.2”);
System.out.println(“您輸入的金額(小寫)為:” + value);
System.out.println(“您輸入的金額(大寫)為:” + convertMoney(value));
}
}
java實現金額轉換,阿拉伯數字的金額轉換成中國傳統的形式
直接通過以下介面類方法實現即可:
import java.math.BigDecimal;
/**
* 金額工具類
*
* @author zn
*
* @Date 2013-2-1
* @Email zn.share@gmail.com
*/
public class MoneyUtil {
private static final int DFT_SCALE = 2;
/** 大寫數字 */
private static final String[] NUMBERS = { “零”, “壹”, “貳”, “叄”, “肆”, “伍”,
“陸”, “柒”, “捌”, “玖” };
/** 整數部分的單位 */
private static final String[] IUNIT = { “元”, “拾”, “佰”, “仟”, “萬”, “拾”, “佰”,
“仟”, “億”, “拾”, “佰”, “仟”, “萬”, “拾”, “佰”, “仟” };
/** 小數部分的單位 */
private static final String[] DUNIT = { “角”, “分”, “厘” };
/**
* 得到大寫金額。
*/
public static String toChinese(String str) {
str = str.replaceAll(“,”, “”);// 去掉”,”
String integerStr;// 整數部分數字
String decimalStr;// 小數部分數字
// 初始化:分離整數部分和小數部分
if (str.indexOf(“.”) 0) {
integerStr = str.substring(0, str.indexOf(“.”));
decimalStr = str.substring(str.indexOf(“.”) + 1);
} else if (str.indexOf(“.”) == 0) {
integerStr = “”;
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = “”;
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分捨去)
if (!integerStr.equals(“”)) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals(“0”)) {
integerStr = “”;
}
}
// overflow超出處理能力,直接返回
if (integerStr.length() IUNIT.length) {
System.out.println(str + “:超出處理能力”);
return str;
}
int[] integers = toArray(integerStr);// 整數部分數字
boolean isMust5 = isMust5(integerStr);// 設置萬單位
int[] decimals = toArray(decimalStr);// 小數部分數字
return getChineseInteger(integers, isMust5)
+ getChineseDecimal(decimals);
}
/**
* 整數部分和小數部分轉換為數組,從高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}
/**
* 得到中文金額的整數部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer(“”);
int length = integers.length;
for (int i = 0; i length; i++) {
// 0出現在關鍵位置:1234(萬)5678(億)9012(萬)3456(元)
// 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元)
String key = “”;
if (integers[i] == 0) {
if ((length – i) == 13)// 萬(億)(必填)
key = IUNIT[4];
else if ((length – i) == 9)// 億(必填)
key = IUNIT[8];
else if ((length – i) == 5 isMust5)// 萬(不必填)
key = IUNIT[4];
else if ((length – i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0時補零,不包含最後一位
if ((length – i) 1 integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key
: (NUMBERS[integers[i]] + IUNIT[length – i – 1]));
}
return chineseInteger.toString();
}
/**
* 得到中文金額的小數部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer(“”);
for (int i = 0; i decimals.length; i++) {
// 捨去3位小數之後的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? “”
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}
/**
* 判斷第5位數字的單位”萬”是否應加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length 4) {
String subInteger = “”;
if (length 8) { // TODO 12-9-17
// 取得從低位數,第5到第8位的字串
subInteger = integerStr.substring(length – 8, length – 4);
} else {
subInteger = integerStr.substring(0, length – 4);
}
return Integer.parseInt(subInteger) 0;
} else {
return false;
}
}
/**
* BigDecimal 相乘,四捨五入保留0位
*
* @param a
* @param b
* @return a*b
*/
public static BigDecimal mutiply(String a, String b, int roundingMode) {
BigDecimal bd = new BigDecimal(a);
return bd.multiply(new BigDecimal(b)).setScale(DFT_SCALE, roundingMode);
}
/**
* BigDecimal 相除,四捨五入保留兩位
*
* @param a
* @param b
* @return a/b
*/
public static BigDecimal div(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
return decimal1.divide(decimal2, DFT_SCALE, roundingMode);
}
/**
* BigDecimal 相加,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sum(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat(“#0.00”);
return decimal1.add(decimal2).setScale(DFT_SCALE, roundingMode);
}
/**
* BigDecimal 相減,四捨五入保留兩位
*
* @param a
* @param b
* @return a+b
*/
public static BigDecimal sub(String a, String b, int roundingMode) {
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
// DecimalFormat format = new DecimalFormat(“#0.00”);
return decimal1.subtract(decimal2).setScale(DFT_SCALE, roundingMode);
}
/**
* 100.00 為10000
*
* @param a
* @return
*/
public static BigDecimal format(String a, int roundingMode) {
return new BigDecimal(a).multiply(new BigDecimal(100)).setScale(0,
roundingMode);
}
public static void main(String[] args) {
String number = “54452”;
System.out.println(number + ” ” + MoneyUtil.toChinese(number));
number = “30200”;
System.out.println(number + ” ” + MoneyUtil.toChinese(number));
number = “30000.05”;
System.out.println(number + ” ” + MoneyUtil.toChinese(number));
number = “30000.00”;
System.out.println(number + ” ” + MoneyUtil.toChinese(number));
}
}
備註:最後面的main方法是具體的調用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297168.html