|
Posted on 2009-05-15 22:09 Gavin.lee 閱讀(635) 評論(1) 編輯 收藏 所屬分類: java SE & EE
摘 : http://www.java2000.net/p32
import java.math.BigDecimal;
 public class ConvertNumber {
 /** *//** 定義數(shù)組存放數(shù)字對應(yīng)的大寫 */
 private final static String[] STR_NUMBER = { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" };
 /** *//** 定義數(shù)組存放位數(shù)的大寫 */
 private final static String[] STR_MODIFY = { "", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟" };
 /** *//**
* 轉(zhuǎn)化整數(shù)部分
*
* @param tempString
* @return 返回整數(shù)部分
*/
 private static String getInteger(String tempString) {
 /** *//** 用來保存整數(shù)部分數(shù)字串 */
String strInteger = null;//
 /** *//** 記錄"."所在位置 */
int intDotPos = tempString.indexOf(".");
int intSignPos = tempString.indexOf("-");
if (intDotPos == -1)
intDotPos = tempString.length();
 /** *//** 取出整數(shù)部分 */
strInteger = tempString.substring(intSignPos + 1, intDotPos);
strInteger = new StringBuffer(strInteger).reverse().toString();
StringBuffer sbResult = new StringBuffer();
 for (int i = 0; i < strInteger.length(); i++) {
sbResult.append(STR_MODIFY[i]);
sbResult.append(STR_NUMBER[strInteger.charAt(i) - 48]);
}
sbResult = sbResult.reverse();
replace(sbResult, "零拾", "零");
replace(sbResult, "零佰", "零");
replace(sbResult, "零仟", "零");
replace(sbResult, "零萬", "萬");
replace(sbResult, "零億", "億");
replace(sbResult, "零零", "零");
replace(sbResult, "零零零", "零");
 /** *//** 這兩句不能顛倒順序 */
replace(sbResult, "零零零零萬", "");
replace(sbResult, "零零零零", "");
 /** *//** 這樣讀起來更習慣. */
replace(sbResult, "壹拾億", "拾億");
replace(sbResult, "壹拾萬", "拾萬");
 /** *//** 刪除個位上的零 */
if (sbResult.charAt(sbResult.length() - 1) == '零' && sbResult.length() != 1)
sbResult.deleteCharAt(sbResult.length() - 1);
 if (strInteger.length() == 2) {
replace(sbResult, "壹拾", "拾");
}
 /** *//** 將結(jié)果反轉(zhuǎn)回來. */
return sbResult.toString();
}
 /** *//**
* 轉(zhuǎn)化小數(shù)部分 例:輸入22.34返回叁肆
*
* @param tempString
* @return
*/
 private static String getFraction(String tempString) {
String strFraction = null;
int intDotPos = tempString.indexOf(".");
 /** *//** 沒有點說明沒有小數(shù),直接返回 */
if (intDotPos == -1)
return "";
strFraction = tempString.substring(intDotPos + 1);
StringBuffer sbResult = new StringBuffer(strFraction.length());
 for (int i = 0; i < strFraction.length(); i++) {
sbResult.append(STR_NUMBER[strFraction.charAt(i) - 48]);
}
return sbResult.toString();
}
 /** *//**
* 判斷傳入的字符串中是否有.如果有則返回點
*
* @param tempString
* @return
*/
 private static String getDot(String tempString) {
return tempString.indexOf(".") != -1 ? "點" : "";
}
 /** *//**
* 判斷傳入的字符串中是否有-如果有則返回負
*
* @param tempString
* @return
*/
 private static String getSign(String tempString) {
return tempString.indexOf("-") != -1 ? "負" : "";
}
 /** *//**
* 將一個數(shù)字轉(zhuǎn)化為金額
*
* @param tempNumber 傳入一個double的變量
* @return 返一個轉(zhuǎn)換好的字符串
*/
 public static String numberToChinese(double tempNumber) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#.#########");
String pTemp = String.valueOf(df.format(tempNumber));
StringBuffer sbResult = new StringBuffer(getSign(pTemp) + getInteger(pTemp) + getDot(pTemp) + getFraction(pTemp));
return sbResult.toString();
}
 public static String numberToChinese(BigDecimal tempNumber) {
return numberToChinese(tempNumber.doubleValue());
}
 /** *//**
* 替代字符
*
* @param pValue
* @param pSource
* @param pDest
*/
 private static void replace(StringBuffer pValue, String pSource, String pDest) {
if (pValue == null || pSource == null || pDest == null)
return;
 /** *//** 記錄pSource在pValue中的位置 */
int intPos = 0;
 do {
intPos = pValue.toString().indexOf(pSource);
 /** *//** 沒有找到pSource */
if (intPos == -1)
break;
pValue.delete(intPos, intPos + pSource.length());
pValue.insert(intPos, pDest);
} while (true);
}
 /** *//**
* @param args
*/
 public static void main(String[] args) {
System.out.println(numberToChinese(1230400567.8934));
}
}

評論
# re: java 將數(shù)字金額轉(zhuǎn)化為漢字(摘) 回復(fù) 更多評論
2012-09-19 15:34 by
好機會
|