金額數值轉換為中文大寫
/**
* Name: net.uni.util.MoneyUtil.java
* Version: 1.0
* Date: 2012-7-2
* Author: 孫偉
*/
package net.uni.util;
/**
* 處理內容:金額數字轉中文大寫
* @version: 1.0
* @see:net.uni.util.MoneyUtil.java
* @date:2012-7-2
* @author:孫偉
*/
public class MoneyUtil {
/** 大寫數字 */
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 isMust = isMust5(integerStr);// 設定萬單位
int[] decimals = toArray(decimalStr);// 小數部分數字
return getChineseInteger(integers, isMust) + 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) {
// 取得從低位數,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}
public static void main(String[] args) {
String number = "1.23";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "1234567890123456.123";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "0.0798";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "10,001,000.09";
System.out.println(number + " " + MoneyUtil.toChinese(number));
number = "01.107700";
System.out.println(number + " " + MoneyUtil.toChinese(number));
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25261409/viewspace-1058700/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript將輸入的數字金額轉換成對應的中文大寫金額JavaScript
- 將金錢數額轉換為大寫
- 小寫金額轉換為大寫
- 將金錢的金額轉換為大寫形式
- 金額大寫轉換(轉)
- 大寫金額轉換 (轉)
- 數字轉換為中文大寫例項程式碼
- 阿拉伯數字轉換成金額大寫金額(包括小數)
- jquery金額數字轉為大寫數字jQuery
- Python數字轉換中文大寫Python
- 金額大小寫轉換(3)
- 金額大寫轉換(摘)
- 英文大小寫轉換
- 可能是最簡潔明瞭的數字金額中文大寫函式 (轉)函式
- 金額轉換
- 數值轉換為字元字元
- plsql小寫金額轉大寫金額函式SQL函式
- js金錢轉換為小額的功能JS
- 將一般的數值轉換為金額格式(分隔千分位和自動增加小數點)
- 小寫轉大寫金額[SQL SERVER] (轉)SQLServer
- Python將阿拉伯數字轉化為中文大寫Python
- 小寫轉大寫金額在C++中的實現 (轉)C++
- asp.net 寫一個RMB金額大寫轉換器(原始碼)ASP.NET原始碼
- Excel金額小寫轉大寫公式Excel公式
- 金額轉換出現的BUG
- 轉換金額大寫的SHELL程式(絕對透過驗證)(轉)
- JavaScript 小寫數字轉換為大寫JavaScript
- JavaScript 人民幣金額格式轉換JavaScript
- 阿拉伯數字金額轉漢語金額- Swift版本Swift
- js將小寫數字轉換為大寫形式JS
- PHP 字串強制轉換為數值問題PHP字串
- javascript將引數轉換為數值程式碼詳解JavaScript
- 小寫轉大寫金額儲存過程[SQL SERVER]儲存過程SQLServer
- 【踩坑系列】使用long型別處理金額,科學計數法導致金額轉大寫異常型別
- JavaScript人民幣金額格式轉換詳解JavaScript
- 1.金額數字轉字元型別字元型別
- Laravel-Excel 3 匯出值轉換數字為文字LaravelExcel
- Swift3.0語言教程字串轉換為數字值Swift字串