导航:首页 > 编程语言 > java金额转换

java金额转换

发布时间:2023-09-13 17:52:37

java实现金额转换,阿拉伯数字的金额转换成中国传统的形式

直接通过以下接口类方法实现即可:
import java.math.BigDecimal;
/**
* 金额工具类
*
* @author zn
*
* @Date 2013-2-1
* @Email [email protected]
*/
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方法是具体的调用。

② java转换数字以万为单位

import java.util.Scanner;

/**

* 小于100000的不转换,大于或等于100000的转换为10万,以此类推,110000转为11万,112000为11.2万

* @author inferno

*

*/

public class Wan {


public static void main(String[] args) {

System.out.print("输入一个整数:");

Scanner scan = new Scanner(System.in);

long num = scan.nextLong();

if(num<100000){

System.out.println("您输入的数字为:"+num);

}else{

double n = (double)num/10000;

System.out.println("您输入的数字为:"+n+"万");

}

}

}

③ Java将控制台输入的人民币数字金额转化为大写

代码如下:
/**
* 人民币转成大写
*
* @param value
* @return String
*/
public static String hangeToBig(double value)
{
char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
char[] vunit = { '万', '亿' }; // 段名表示
char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
long midVal = (long) (value * 100); // 转化成整形
String valStr = String.valueOf(midVal); // 转化成字符串
String head = valStr.substring(0, valStr.length() - 2); // 取整数部分
String rail = valStr.substring(valStr.length() - 2); // 取小数部分
String prefix = ""; // 整数部分转化的结果
String suffix = ""; // 小数部分转化的结果
// 处理小数点后面的数
if (rail.equals("00"))
{ // 如果小数部分为0
suffix = "整";
}
else
{
suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
}
// 处理小数点前面的数
char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
char zero = '0'; // 标志'0'表示出现过0
byte zeroSerNum = 0; // 连续出现0的次数
for (int i = 0; i < chDig.length; i++)
{ // 循环处理每个数字
int idx = (chDig.length - i - 1) % 4; // 取段内位置
int vidx = (chDig.length - i - 1) / 4; // 取段位置
if (chDig[i] == '0')
{ // 如果当前字符是0
zeroSerNum++; // 连续0次数递增
if (zero == '0')
{ // 标志
zero = digit[0];
}
else if (idx == 0 && vidx > 0 && zeroSerNum < 4)
{
prefix += vunit[vidx - 1];
zero = '0';
}
continue;
}
zeroSerNum = 0; // 连续0次数清零
if (zero != '0')
{ // 如果标志不为0,则加上,例如万,亿什么的
prefix += zero;
zero = '0';
}
prefix += digit[chDig[i] - '0']; // 转化该数字表示
if (idx > 0)
prefix += hunit[idx - 1];
if (idx == 0 && vidx > 0)
{
prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿
}
}
if (prefix.length() > 0)
prefix += '圆'; // 如果整数部分存在,则有圆的字样
return prefix + suffix; // 返回正确表示
}

④ Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)

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 = "万"; 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))); }
//拾佰仟万亿等都是汉字里面才有的单位,加上它们 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("零万") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 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); cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO); cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN); if (cMoneyStringBuffer.indexOf("零角零分") != -1)//没有零头,加整
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"), cMoneyStringBuffer.length(), "整"); else if (cMoneyStringBuffer.indexOf("零分") != -1)//没有零分,加整 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"), cMoneyStringBuffer.length(), "整");
else { if(cMoneyStringBuffer.indexOf("零角")!=-1)
cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),cMoneyStringBuffer.indexOf("零角")+2);
tmpBuffer.append("整"); } result = cMoneyStringBuffer.toString(); return result; } private void checkPrecision(String moneyStr) {
int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1; if (fractionDigits > 2) throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); //精度不能比分低 }public static void main(String args[]) { System.out.println(getInstance().format(new Double(10010001.01))); }}

⑤ java如何把大写的金额改成小写

/**
* @(#)SimpleMoneyFormat.java
*
*金额转换,阿拉伯数字的金额转换为中国传统的形式输出
* @lpxuan
* @version 1.00 2010/7/31
*/

import java.text.NumberFormat;
import java.util.HashMap;
import java.lang.StringBuffer;

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="万";
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 chineseMoenyPattern=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);

chineseMoenyPattern.put("1",TEN);
chineseMoenyPattern.put("2",HUNDRED);
chineseMoenyPattern.put("3",THOUSAND);
chineseMoenyPattern.put("4",TEN_THOUSAND);
chineseMoenyPattern.put("5",TEN);
chineseMoenyPattern.put("6",HUNDRED);
chineseMoenyPattern.put("7",THOUSAND);
chineseMoenyPattern.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=addUnitsToChineseMoenyString(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)));
}

int indexOfDot=cMoneyStringBuffer.indexOf(DOT);
int moneyPatternCursor=1;
for(int i=indexOfDot-1;i>0;i--){
cMoneyStringBuffer.insert(i,
chineseMoenyPattern.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("零万")!=-1){
cMoneyStringBuffer.replace(
cMoneyStringBuffer.indexOf("零万"),
cMoneyStringBuffer.indexOf("零万")+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 addUnitsToChineseMoenyString(String moneyStr){
String result;
StringBuffer cMoneyStringBuffer=new StringBuffer(moneyStr);

int indexOfDot=cMoneyStringBuffer.indexOf(DOT);
cMoneyStringBuffer.replace(indexOfDot,indexOfDot+1,YUAN);
cMoneyStringBuffer.insert(cMoneyStringBuffer.length()-1,JIAO);
cMoneyStringBuffer.insert(cMoneyStringBuffer.length(),FEN);

//拾佰仟万亿等都是汉字里面才有的单位,加上它们
if(cMoneyStringBuffer.indexOf("零角零分")!=-1)//去掉零头,加整
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"),
cMoneyStringBuffer.length(),"整");
else if(cMoneyStringBuffer.indexOf("零分")!=-1)//去掉零分,加整
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"),
cMoneyStringBuffer.length(),"整");
else if(cMoneyStringBuffer.indexOf("零角")!=-1)
cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),
cMoneyStringBuffer.indexOf("零角")+2);
result=cMoneyStringBuffer.toString();
return result;
}

//检查精度的合法性
private void checkPrecision(String moneyStr){
int fractionDigits=moneyStr.length()-moneyStr.indexOf(DOT)-1;
if(fractionDigits>2)//精度不能比分低
throw new RuntimeException("金额"+moneyStr+"的小数位数多余两位.");
}

public static void main(String[] args){
System.out.println(getInstance().format(new Double(10010001.01)));
}

}

⑥ 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;i<str.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;i<ch.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如何将数字转为中文大写

import org.apache.commons.lang3.StringUtils;

/**
* @Title: ConvertUpMoney
* @Description: 将数字金额转换为大写中文金额
* @date: 2019年6月18日 下午10:52:27
*/
public class ConvertUpMoney {

// 大写数字
private static final String[] NUMBERS = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
// 整数部分的单位
private static final String[] IUNIT = {"元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万","拾","佰","仟"};
// 小数部分的单位
private static final String[] DUNIT = {"角","分","厘"};

/**
* 转换为大写的中文金额
* @param str 字符串类型的 金额数字
* @return
*/
public static String toChinese(String str) {
// 判断输入的金额字符串是否符合要求
if (StringUtils.isBlank(str) || !str.matches("(-)?[\\d]*(.)?[\\d]*")) {
return "抱歉,请输入数字!";
}

if("0".equals(str) || "0.00".equals(str) || "0.0".equals(str)) {
return "零元";
}

// 判断金额数字中是否存在负号"-"
boolean flag = false;
if(str.startsWith("-")){
// 标志位,标志此金额数字为负数
flag = true;
str = str.replaceAll("-", "");
}

// 去掉金额数字中的逗号","
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 = "";
}

// beyond超出计算能力,直接返回
if(integerStr.length()>IUNIT.length) {
return "超出计算能力!";
}

// 整数部分数字
int[] integers = toIntArray(integerStr);
// 判断整数部分是否存在输入012的情况
if (integers.length>1 && integers[0] == 0) {
return "抱歉,输入数字不符合要求!";
}
// 设置万单位
boolean isWan = isWan5(integerStr);
// 小数部分数字
int[] decimals = toIntArray(decimalStr);
// 返回最终的大写金额
String result = getChineseInteger(integers, isWan) + getChineseDecimal(decimals);
if(flag){
// 如果是负数,加上"负"
return "负" + result;
}else{
return result;
}
}

/**
* 将字符串转为int数组
* @param number 数字
* @return
*/
private static int[] toIntArray(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;
}

/**
* 将整数部分转为大写的金额
* @param integers 整数部分数字
* @param isWan 整数部分是否已经是达到【万】
* @return
*/
public static String getChineseInteger(int[] integers,boolean isWan) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
if (length == 1 && integers[0] == 0) {
return "";
}
for(int i=0; i<length; i++) {
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 && isWan) {//万
key = IUNIT[4];
}else if((length - i) == 1) {//元
key = IUNIT[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();
}

/**
* 将小数部分转为大写的金额
* @param decimals 小数部分的数字
* @return
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for(int i = 0;i<decimals.length;i++) {
if(i == 3) {
break;
}
chineseDecimal.append(decimals[i]==0?"":(NUMBERS[decimals[i]]+DUNIT[i]));
}
return chineseDecimal.toString();
}

/**
* 判断当前整数部分是否已经是达到【万】
* @param integerStr 整数部分数字
* @return
*/
private static boolean isWan5(String integerStr) {
int length = integerStr.length();
if(length > 4) {
String subInteger = "";
if(length > 8) {
subInteger = integerStr.substring(length- 8,length -4);
}else {
subInteger = integerStr.substring(0,length - 4);
}
return Integer.parseInt(subInteger) > 0;
}else {
return false;
}
}

// Test
public static void main(String[] args) {
String number = "12.56";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "1234567890563886.123";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "1600";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "156,0";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "-156,0";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "0.12";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "0.0";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "01.12";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "0125";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "-0125";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

number = "sdw5655";
System.out.println(number+": "+ConvertUpMoney.toChinese(number));

System.out.println(null+": "+ConvertUpMoney.toChinese(null));
}
}

阅读全文

与java金额转换相关的资料

热点内容
outlook命令 浏览:229
程序员那么可爱主角介绍 浏览:932
银行卡短信消息如何加密 浏览:246
文件夹怎么不重名 浏览:406
linuxyum安装java 浏览:250
java数字计算 浏览:286
java按钮文字 浏览:641
python列表互换位置 浏览:337
sw怎么删除定向命令 浏览:757
php包含数组元素 浏览:666
安卓系统开发app需要什么 浏览:730
ssh2项目源码 浏览:288
三星提供了什么服务器地址 浏览:903
阿里云轻量应用服务器60元 浏览:160
微信公众号支付java 浏览:217
虾皮用的什么服务器 浏览:144
拍照的app哪个好用 浏览:890
方舟编译器2022 浏览:770
一般情况下源码注释量 浏览:743
18号命令 浏览:871