本文目录一览:
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方法是具体的调用。