java实验金额转换,java金额转换
/**
* @author wu 將一個數字轉化為金額
*/
public class ConvertNumber {
/** 定義數組存放數字對應的大寫 */
private final static String[] STR_NUMBER = { "零", "壹", "貳", "叁", "肆", "伍",
"陸", "柒", "捌", "玖" };
/** 定義數組存放位數的大寫 */
private final static String[] STR_MODIFY = { "", "拾", "佰", "仟", "萬", "拾",
"佰", "仟", "億", "拾", "佰", "仟" };
/**
* 轉化整數部分
*
* @param tempString
* @return 返回整數部分
*/
private static String getInteger(String tempString) {
/** 用來保存整數部分數字串 */
String strInteger = null;//
/** 記錄"."所在位置 */
int intDotPos = tempString.indexOf(".");
int intSignPos = tempString.indexOf("-");
if (intDotPos == -1)
intDotPos = tempString.length();
/** 取出整數部分 */
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, "壹拾", "拾");
}
/** 將結果反轉回來. */
return sbResult.toString();
}
/**
* 轉化小數部分 例:輸入22.34返回叁肆
*
* @param tempString
* @return
*/
private static String getFraction(String tempString) {
String strFraction = null;
int intDotPos = tempString.indexOf(".");
/** 沒有點說明沒有小數,直接返回 */
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 ? "負" : "";
}
/**
* 將一個數字轉化為金額
*
* @param tempNumber
*??????????? 傳入一個double的變量
* @return 返一個轉換好的字符串
*/
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();
}
/**
* 替代字符
*
* @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) {
// TODO Auto-generated method stub
}
}
總結
以上是生活随笔為你收集整理的java实验金额转换,java金额转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原创:古代最霸气的十大兵器,凤翅镏金镋上
- 下一篇: 今目标——让我们离成功更近……