代碼如下:
為千陽(yáng)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及千陽(yáng)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、千陽(yáng)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
/**
* 人民幣轉(zhuǎn)成大寫(xiě)
*
* @param value
* @return String
*/
public static String hangeToBig(double value)
{
char[] hunit = { '拾', '佰', '仟' }; // 段內(nèi)位置表示
char[] vunit = { '萬(wàn)', '億' }; // 段名表示
char[] digit = { '零', '壹', '貳', '叁', '肆', '伍', '陸', '柒', '捌', '玖' }; // 數(shù)字表示
long midVal = (long) (value * 100); // 轉(zhuǎn)化成整形
String valStr = String.valueOf(midVal); // 轉(zhuǎn)化成字符串
String head = valStr.substring(0, valStr.length() - 2); // 取整數(shù)部分
String rail = valStr.substring(valStr.length() - 2); // 取小數(shù)部分
String prefix = ""; // 整數(shù)部分轉(zhuǎn)化的結(jié)果
String suffix = ""; // 小數(shù)部分轉(zhuǎn)化的結(jié)果
// 處理小數(shù)點(diǎn)后面的數(shù)
if (rail.equals("00"))
{ // 如果小數(shù)部分為0
suffix = "整";
}
else
{
suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否則把角分轉(zhuǎn)化出來(lái)
}
// 處理小數(shù)點(diǎn)前面的數(shù)
char[] chDig = head.toCharArray(); // 把整數(shù)部分轉(zhuǎn)化成字符數(shù)組
char zero = '0'; // 標(biāo)志'0'表示出現(xiàn)過(guò)0
byte zeroSerNum = 0; // 連續(xù)出現(xiàn)0的次數(shù)
for (int i = 0; i chDig.length; i++)
{ // 循環(huán)處理每個(gè)數(shù)字
int idx = (chDig.length - i - 1) % 4; // 取段內(nèi)位置
int vidx = (chDig.length - i - 1) / 4; // 取段位置
if (chDig[i] == '0')
{ // 如果當(dāng)前字符是0
zeroSerNum++; // 連續(xù)0次數(shù)遞增
if (zero == '0')
{ // 標(biāo)志
zero = digit[0];
}
else if (idx == 0 vidx 0 zeroSerNum 4)
{
prefix += vunit[vidx - 1];
zero = '0';
}
continue;
}
zeroSerNum = 0; // 連續(xù)0次數(shù)清零
if (zero != '0')
{ // 如果標(biāo)志不為0,則加上,例如萬(wàn),億什么的
prefix += zero;
zero = '0';
}
prefix += digit[chDig[i] - '0']; // 轉(zhuǎn)化該數(shù)字表示
if (idx 0)
prefix += hunit[idx - 1];
if (idx == 0 vidx 0)
{
prefix += vunit[vidx - 1]; // 段結(jié)束位置應(yīng)該加上段名如萬(wàn),億
}
}
if (prefix.length() 0)
prefix += '圓'; // 如果整數(shù)部分存在,則有圓的字樣
return prefix + suffix; // 返回正確表示
}
不知道這個(gè)是不是您需要的答案
package test.format;
import java.text.Numberformat;
import java.util.HashMap;
public class SimpleMoneyformat {
public static final String EMPTY = "";
public static final String ZERO = "零";
public static final String ONE = "壹";
public static final String TWO = "貳";
public static final String THREE = "叁";
public static final String FOUR = "肆";
public static final String FIVE = "伍";
public static final String SIX = "陸";
public static final String SEVEN = "柒";
public static final String EIGHT = "捌";
public static final String NINE = "玖";
public static final String TEN = "拾";
public static final String HUNDRED = "佰";
public static final String THOUSAND = "仟";
public static final String TEN_THOUSAND = "萬(wàn)";
public static final String HUNDRED_MILLION = "億";
public static final String YUAN = "元";
public static final String JIAO = "角";
public static final String FEN = "分";
public static final String DOT = ".";
private static SimpleMoneyformat formatter = null;
private HashMap chineseNumberMap = new HashMap();
private HashMap chineseMoneyPattern = new HashMap();
private Numberformat numberformat = Numberformat.getInstance();
private SimpleMoneyformat() {
numberformat.setMaximumFractionDigits(4);
numberformat.setMinimumFractionDigits(2);
numberformat.setGroupingUsed(false);
chineseNumberMap.put("0", ZERO);
chineseNumberMap.put("1", ONE);
chineseNumberMap.put("2", TWO);
chineseNumberMap.put("3", THREE);
chineseNumberMap.put("4", FOUR);
chineseNumberMap.put("5", FIVE);
chineseNumberMap.put("6", SIX);
chineseNumberMap.put("7", SEVEN);
chineseNumberMap.put("8", EIGHT);
chineseNumberMap.put("9", NINE);
chineseNumberMap.put(DOT, DOT);
chineseMoneyPattern.put("1", TEN);
chineseMoneyPattern.put("2", HUNDRED);
chineseMoneyPattern.put("3", THOUSAND);
chineseMoneyPattern.put("4", TEN_THOUSAND);
chineseMoneyPattern.put("5", TEN);
chineseMoneyPattern.put("6", HUNDRED);
chineseMoneyPattern.put("7", THOUSAND);
chineseMoneyPattern.put("8", HUNDRED_MILLION);
}
public static SimpleMoneyformat getInstance() {
if (formatter == null)
formatter = new SimpleMoneyformat();
return formatter;
}
public String format(String moneyStr) {
checkPrecision(moneyStr);
String result;
result = convertToChineseNumber(moneyStr);
result = addUnitsToChineseMoneyString(result);
return result;
}
public String format(double moneyDouble) {
return format(numberformat.format(moneyDouble));
}
public String format(int moneyInt) {
return format(numberformat.format(moneyInt));
}
public String format(long moneyLong) {
return format(numberformat.format(moneyLong));
}
public String format(Number moneyNum) {
return format(numberformat.format(moneyNum));
}
private String convertToChineseNumber(String moneyStr) {
String result;
StringBuffer cMoneyStringBuffer = new StringBuffer();
for (int i = 0; i moneyStr.length(); i++) {
cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));
}
//拾佰仟萬(wàn)億等都是漢字里面才有的單位,加上它們
int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
int moneyPatternCursor = 1;
for (int i = indexOfDot - 1; i 0; i--) {
cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1;
}
String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));
cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());
while (cMoneyStringBuffer.indexOf("零拾") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
}
while (cMoneyStringBuffer.indexOf("零佰") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
}
while (cMoneyStringBuffer.indexOf("零仟") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
}
while (cMoneyStringBuffer.indexOf("零萬(wàn)") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零萬(wàn)"), cMoneyStringBuffer.indexOf("零萬(wàn)") + 2, TEN_THOUSAND);
}
while (cMoneyStringBuffer.indexOf("零億") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零億"), cMoneyStringBuffer.indexOf("零億") + 2, HUNDRED_MILLION);
}
while (cMoneyStringBuffer.indexOf("零零") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
}
if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length());
cMoneyStringBuffer.append(fractionPart);
result = cMoneyStringBuffer.toString();
return result;
}
private String addUnitsToChineseMoneyString(String moneyStr) {
String result;
StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);
private static String chainNum[] = { "零", "壹", "貳", "叁", "肆", "伍", "陸",
"柒", "捌", "玖", "拾", "元", "角", "分", "佰", "仟", "萬(wàn)", "億" };
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) + "萬(wàn)";
}
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位數(shù)
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里面 然后調(diào)用
num2chainNum("11231.120")
/**
* 金額小數(shù)轉(zhuǎn)換成中文大寫(xiě)金額
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "萬(wàn)", "千", "佰", "拾", "億", "千", "佰",
"拾", "萬(wàn)", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "貳", "叁", "肆", "伍", "陸",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 將金額小數(shù)轉(zhuǎn)換成中文大寫(xiě)金額
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money 0 || money MAX_VALUE)
return "參數(shù)非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于選擇金額數(shù)值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于選擇金額單位
boolean isZero = false; // 用于判斷當(dāng)前為是否為零
String result = "";
for (; numIndex strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "億" || UNIT[unitIndex] == "萬(wàn)"
|| UNIT[unitIndex] == "元") { // 如果當(dāng)前位是億、萬(wàn)、元,且數(shù)值為零
result = result + UNIT[unitIndex]; //補(bǔ)單位億、萬(wàn)、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分結(jié)尾就加"整"字
if (!result.endsWith("角")!result.endsWith("分")) {
result = result + "整";
}
//例如沒(méi)有這行代碼,數(shù)值"400000001101.2",輸出就是"肆千億萬(wàn)壹千壹佰零壹元貳角"
result = result.replaceAll("億萬(wàn)", "億");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您輸入的金額(小寫(xiě))為:" + value);
System.out.println("您輸入的金額(大寫(xiě))為:" + convertMoney(value));
}
}
直接通過(guò)以下接口類(lèi)方法實(shí)現(xiàn)即可:
import java.math.BigDecimal;
/**
* 金額工具類(lèi)
*
* @author zn
*
* @Date 2013-2-1
* @Email zn.share@gmail.com
*/
public class MoneyUtil {
private static final int DFT_SCALE = 2;
/** 大寫(xiě)數(shù)字 */
private static final String[] NUMBERS = { "零", "壹", "貳", "叁", "肆", "伍",
"陸", "柒", "捌", "玖" };
/** 整數(shù)部分的單位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "萬(wàn)", "拾", "佰",
"仟", "億", "拾", "佰", "仟", "萬(wàn)", "拾", "佰", "仟" };
/** 小數(shù)部分的單位 */
private static final String[] DUNIT = { "角", "分", "厘" };
/**
* 得到大寫(xiě)金額。
*/
public static String toChinese(String str) {
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整數(shù)部分?jǐn)?shù)字
String decimalStr;// 小數(shù)部分?jǐn)?shù)字
// 初始化:分離整數(shù)部分和小數(shù)部分
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);// 整數(shù)部分?jǐn)?shù)字
boolean isMust5 = isMust5(integerStr);// 設(shè)置萬(wàn)單位
int[] decimals = toArray(decimalStr);// 小數(shù)部分?jǐn)?shù)字
return getChineseInteger(integers, isMust5)
+ getChineseDecimal(decimals);
}
/**
* 整數(shù)部分和小數(shù)部分轉(zhuǎn)換為數(shù)組,從高位至低位
*/
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;
}
/**
* 得到中文金額的整數(shù)部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i length; i++) {
// 0出現(xiàn)在關(guān)鍵位置:1234(萬(wàn))5678(億)9012(萬(wàn))3456(元)
// 特殊情況:10(拾元、壹拾元、壹拾萬(wàn)元、拾萬(wàn)元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 萬(wàn)(億)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 億(必填)
key = IUNIT[8];
else if ((length - i) == 5 isMust5)// 萬(wàn)(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0時(shí)補(bǔ)零,不包含最后一位
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();
}
/**
* 得到中文金額的小數(shù)部分。
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i decimals.length; i++) {
// 舍去3位小數(shù)之后的
if (i == 3)
break;
chineseDecimal.append(decimals[i] == 0 ? ""
: (NUMBERS[decimals[i]] + DUNIT[i]));
}
return chineseDecimal.toString();
}
/**
* 判斷第5位數(shù)字的單位"萬(wàn)"是否應(yīng)加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length 4) {
String subInteger = "";
if (length 8) { // TODO 12-9-17
// 取得從低位數(shù),第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方法是具體的調(diào)用。
文章名稱(chēng):java代碼將金額轉(zhuǎn)換,java string轉(zhuǎn)金額
URL標(biāo)題:http://chinadenli.net/article38/heidsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站、網(wǎng)站收錄、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)