不知道這個是不是您需要的答案
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);
❷ 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編寫一程序將一個int數字轉移成中文數字的金額(如:13099 -> 一萬三千零九十九)
我寫過一個 數字轉人民幣金額的程序,這個程序挺啰嗦的,不過倒是符合人民幣大寫規范
代碼如下,經過部分測試,符合人民幣書寫方式,如果還有問題,請給我發消息,我再修改
public class Num2Rmb {
private String[] hanArr = {"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
private String[] unitArr = {"分","角","元","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬"};
private String divide(double num){
long zheng = (long) num ;
long xiao = Math.round((num - zheng) * 100);
System.out.println(String.valueOf(zheng));
System.out.println(xiao < 10?"0"+String.valueOf(xiao):String.valueOf(xiao));
String temp = String.valueOf(zheng) + (xiao < 10?"0"+String.valueOf(xiao):String.valueOf(xiao));
System.out.println(temp);
return temp;
}
private String toHanStr(String numStr){
String result = "";
int numLen = numStr.length();
long j = Long.parseLong(numStr);
if (j == 0){
result = "零元";
return result;
}
int temp = 0;
for(int i = 0; i < numLen; i++){
int num = numStr.charAt(i) - 48;
if( num != 0 ){ //如果數字不為0,直接變成數字與單位
result += hanArr[num] + unitArr[numLen -1 -i];
}
else if (num == 0 && i==0 ){ //如果數字為0,且是第一位,直接變成零
result += hanArr[num];
}
else if (num == 0 && result.charAt(result.length() -1) != '零' ){
result += hanArr[num]; //如果數字為0,且前面不是『零』,保證只有1個零,類似1001元
}
if ( i == numLen -11 && result.charAt(result.length() -1) == '零'){
result = result.substring(0,result.length() -1) + "億";
}
if ( i == numLen -7 && result.charAt(result.length() -1) == '零'){
result = result.substring(0,result.length() -1) + "萬";
}
if ( i == numLen -3 && result.charAt(result.length() -1) == '零'){
result = result.substring(0,result.length() -1) + "元";
}
}
temp = result.lastIndexOf("萬");
if (temp != -1 && result.charAt(temp - 1) == '億'){
result = result.substring(0, temp) + result.substring(temp+1);
}
temp = result.indexOf("元");
if (temp ==0 ){
result = result.substring(1);
}
temp = result.indexOf("零");
if (temp ==0 ){
result = result.substring(1);
}
if (result.charAt(result.length() - 1) == '零' )
result = result.substring(0,result.length() - 1);
if (result.charAt(result.length() - 1) == '元')
result += "整";
result = "人民幣" + result;
return result;
}
public static void main(String[] args) {
Num2Rmb nr = new Num2Rmb();
String nrStr = nr.divide(1);
System.out.println(nr.toHanStr(nrStr));
}
}
❹ JAVA將數字轉表示成相應的錢
public class ShuceUtils {
/**
* 將數字轉換成中文表示
* @param smallmoney double
* @return String
*/
public static String TranslateMoneyToChn(double smallmoney) {
String value = String.valueOf(smallmoney);
if (null == value || "".equals(value.trim()))
return "零";
String strCheck, strArr, strFen, strDW, strNum, strBig, strNow;
double d = 0;
try {
d = Double.parseDouble(value);
}
catch (Exception e) {
return "數據" + value + "非法!";
}
strCheck = value + ".";
int dot = strCheck.indexOf(".");
if (dot > 12) {
return "數據" + value + "過大,無法處理!";
}
try {
int i = 0;
strBig = "";
strDW = "";
strNum = "";
long intFen = Math.round(d * 100);
strFen = String.valueOf(intFen);
int lenIntFen = strFen.length();
while (lenIntFen != 0) {
i++;
switch (i) {
case 1:
strDW = "分";
break;
case 2:
strDW = "角";
break;
case 3:
strDW = "圓";
break;
case 4:
strDW = "拾";
break;
case 5:
strDW = "佰";
break;
case 6:
strDW = "仟";
break;
case 7:
strDW = "萬";
break;
case 8:
strDW = "拾";
break;
case 9:
strDW = "佰";
break;
case 10:
strDW = "仟";
break;
case 11:
strDW = "億";
break;
case 12:
strDW = "拾";
break;
case 13:
strDW = "佰";
break;
case 14:
strDW = "仟";
break;
}
switch (strFen.charAt(lenIntFen - 1)) { //選擇數字
case '1':
strNum = "壹";
break;
case '2':
strNum = "貳";
break;
case '3':
strNum = "叄";
break;
case '4':
strNum = "肆";
break;
case '5':
strNum = "伍";
break;
case '6':
strNum = "陸";
break;
case '7':
strNum = "柒";
break;
case '8':
strNum = "捌";
break;
case '9':
strNum = "玖";
break;
case '0':
strNum = "零";
break;
}
//處理特殊情況
strNow = strBig;
//分為零時的情況
if ( (i == 1) && (strFen.charAt(lenIntFen - 1) == '0'))
strBig = "整";
//角為零時的情況
else if ( (i == 2) && (strFen.charAt(lenIntFen - 1) == '0')) { //角分同時為零時的情況
if (!strBig.equals("整"))
strBig = "零" + strBig;
}
//元為零的情況
else if ( (i == 3) && (strFen.charAt(lenIntFen - 1) == '0'))
strBig = "圓" + strBig;
//拾-仟中一位為零且其前一位(元以上)不為零的情況時補零
else if ( (i < 7) && (i > 3) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) != '零') && (strNow.charAt(0) != '圓'))
strBig = "零" + strBig;
//拾-仟中一位為零且其前一位(元以上)也為零的情況時跨過
else if ( (i < 7) && (i > 3) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '零')) {}
//拾-仟中一位為零且其前一位是元且為零的情況時跨過
else if ( (i < 7) && (i > 3) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '圓')) {}
//當萬為零時必須補上萬字
else if ( (i == 7) && (strFen.charAt(lenIntFen - 1) == '0'))
strBig = "萬" + strBig;
//拾萬-仟萬中一位為零且其前一位(萬以上)不為零的情況時補零
else if ( (i < 11) && (i > 7) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) != '零') && (strNow.charAt(0) != '萬'))
strBig = "零" + strBig;
//拾萬-仟萬中一位為零且其前一位(萬以上)也為零的情況時跨過
else if ( (i < 11) && (i > 7) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '萬')) {}
//拾萬-仟萬中一位為零且其前一位為萬位且為零的情況時跨過
else if ( (i < 11) && (i > 7) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '零')) {}
//萬位為零且存在仟位和十萬以上時,在萬仟間補零
else if ( (i < 11) && (i > 8) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '萬') && (strNow.charAt(2) == '仟'))
strBig = strNum + strDW + "萬零" + strBig.substring(1, strBig.length());
//單獨處理億位
else if (i == 11) {
//億位為零且萬全為零存在仟位時,去掉萬補為零
if ( (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '萬') && (strNow.charAt(2) == '仟'))
strBig = "億" + "零" + strBig.substring(1, strBig.length());
//億位為零且萬全為零不存在仟位時,去掉萬
else if ( (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '萬') && (strNow.charAt(2) != '仟'))
strBig = "億" + strBig.substring(1, strBig.length());
//億位不為零且萬全為零存在仟位時,去掉萬補為零
else if ( (strNow.charAt(0) == '萬') && (strNow.charAt(2) == '仟'))
strBig = strNum + strDW + "零" + strBig.substring(1, strBig.length());
//億位不為零且萬全為零不存在仟位時,去掉萬
else if ( (strNow.charAt(0) == '萬') && (strNow.charAt(2) != '仟'))
strBig = strNum + strDW + strBig.substring(1, strBig.length());
//其他正常情況
else
strBig = strNum + strDW + strBig;
}
//拾億-仟億中一位為零且其前一位(億以上)不為零的情況時補零
else if ( (i < 15) && (i > 11) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) != '零') && (strNow.charAt(0) != '億'))
strBig = "零" + strBig;
//拾億-仟億中一位為零且其前一位(億以上)也為零的情況時跨過
else if ( (i < 15) && (i > 11) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '億')) {}
//拾億-仟億中一位為零且其前一位為億位且為零的情況時跨過
else if ( (i < 15) && (i > 11) && (strFen.charAt(lenIntFen - 1) == '0') &&
(strNow.charAt(0) == '零')) {}
//億位為零且不存在仟萬位和十億以上時去掉上次寫入的零
else if ( (i < 15) && (i > 11) && (strFen.charAt(lenIntFen - 1) != '0') &&
(strNow.charAt(0) == '零') && (strNow.charAt(1) == '億') &&
(strNow.charAt(3) != '仟'))
strBig = strNum + strDW + strBig.substring(1, strBig.length());
//億位為零且存在仟萬位和十億以上時,在億仟萬間補零
else if ( (i < 15) && (i > 11) && (strFen.charAt(lenIntFen - 1) != '0') &&
(strNow.charAt(0) == '零') && (strNow.charAt(1) == '億') &&
(strNow.charAt(3) == '仟'))
strBig = strNum + strDW + "億零" + strBig.substring(2, strBig.length());
else
strBig = strNum + strDW + strBig;
strFen = strFen.substring(0, lenIntFen - 1);
lenIntFen--;
}
return strBig;
}
catch (Exception e) {
return "";
}
}
public static void main(String[] args){
System.out.println(ShuceUtils.TranslateMoneyToChn(123450000));
}
}
❺ 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 關於中文大寫金額與阿拉伯數字 互相轉換的問題(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]求一個將數字字元串轉換成人民幣讀法的方法();
上午也看到有人問這個,不知道和Lz是不是同一個人~
我直接復制好了,那邊的那個是我寫的,要是有漏洞還希望可以積極指出,我會盡快完善:
package com.ufotable.test;
import java.math.BigDecimal;
public class Test8 {
public enum RMB{
角,分,元,十,百,千,萬W,十萬,百萬,千萬,億Y,十億,百億,千億,萬W億,十萬億,百萬億,千萬億,兆Z,十兆,百兆,千兆,萬W兆,十萬兆,百萬兆,千萬兆,億Y兆
} public enum NUM{
零,壹,貳,叄,肆,伍,陸,柒,玐,玖
}
static String read(Integer rmb){
return read(rmb.toString());
}
static String read(double rmb){
return read(new BigDecimal(rmb).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
}
static String read(Long rmb){
return read(rmb.toString());
}
static String read(String rmb){
String str1=rmb.replaceAll("\\.\\d*", ""),
str2=rmb.replaceAll("[-]|\\d+\\.", ""),
str3="";
if(rmb.charAt(0)=='-'){str1=str1.substring(1);str3="負";}
int i = 0;
while(i<str1.length()){
int j = str1.length()-i+1;
int c=(str1.charAt(i++)-'0')%9;
str3+=NUM.values()[c].name()+
RMB.values()[j];
}
int j=0;
while(j<str2.length()&&j<2){
int c=(str2.charAt(j++)-'0')%9;
str3+=NUM.values()[c].name()+RMB.values()[j-1];
}
str3=str3.replaceAll("萬", "").replaceAll("億", "").replaceAll("兆", "").
replaceAll("零{1}[兆,億,萬,千,百,十,角]{1}", "零").
replaceAll("零+", "零").
replaceAll("W", "萬").
replaceAll("Y", "億").
replaceAll("Z", "兆").
replaceAll("零億", "億").
replaceAll("零萬", "萬").
replaceAll("零兆", "兆").
replaceAll("零元|元零", "元").
replaceAll("零分|元分", "元").
replaceAll("角元", "角");
return str3;
}
public static void main(String[] args) {
System.out.println(read(2411004444500203.405));
}
}
輸出結果:
貳千肆百壹十壹萬零肆十肆億肆千肆百伍十萬零貳百零叄元伍角
❽ 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實現金額轉換,阿拉伯數字的金額轉換成中國傳統的形式
直接通過以下介面類方法實現即可:
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中,怎麼將一段浮點數值轉換成人民幣格式例如將12004567.866轉換成 :壹千貳百萬
直接通過以下介面類方法實現即可:importjava.math.BigDecimal;/***金額工具類**@authorzn**@Date2013-2-1*@[email protected]*/publicclassMoneyUtil{privatestaticfinalintDFT_SCALE=2;/**大寫數字*/privatestaticfinalString[]NUMBERS={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};/**整數部分的單位*/privatestaticfinalString[]IUNIT={"元","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟"};/**小數部分的單位*/privatestaticfinalString[]DUNIT={"角","分","厘"};/***得到大寫金額。*/publicstaticStringtoChinese(Stringstr){str=str.replaceAll(",","");//去掉","StringintegerStr;//整數部分數字StringdecimalStr;//小數部分數字//初始化:分離整數部分和小數部分if(str.indexOf(".")>0){integerStr=str.substring(0,str.indexOf("."));decimalStr=str.substring(str.indexOf(".")+1);}elseif(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+":超出處理能力");returnstr;}int[]integers=toArray(integerStr);//整數部分數字booleanisMust5=isMust5(integerStr);//設置萬單位int[]decimals=toArray(decimalStr);//小數部分數字returngetChineseInteger(integers,isMust5)+getChineseDecimal(decimals);}/***整數部分和小數部分轉換為數組,從高位至低位*/privatestaticint[]toArray(Stringnumber){int[]array=newint[number.length()];for(inti=0;i1&&integers[i+1]!=0)key+=NUMBERS[0];}chineseInteger.append(integers[i]==0?key:(NUMBERS[integers[i]]+IUNIT[length-i-1]));}returnchineseInteger.toString();}/***得到中文金額的小數部分。*/(int[]decimals){StringBufferchineseDecimal=newStringBuffer("");for(inti=0;i4){StringsubInteger="";if(length>8){//TODO12-9-17//取得從低位數,第5到第8位的字串subInteger=integerStr.substring(length-8,length-4);}else{subInteger=integerStr.substring(0,length-4);}returnInteger.parseInt(subInteger)>0;}else{returnfalse;}}/***BigDecimal相乘,四捨五入保留0位**@parama*@paramb*@returna*b*/publicstaticBigDecimalmutiply(Stringa,Stringb,introundingMode){BigDecimalbd=newBigDecimal(a);returnbd.multiply(newBigDecimal(b)).setScale(DFT_SCALE,roundingMode);}/***BigDecimal相除,四捨五入保留兩位**@parama*@paramb*@returna/b*/publicstaticBigDecimaldiv(Stringa,Stringb,introundingMode){BigDecimaldecimal1=newBigDecimal(a);BigDecimaldecimal2=newBigDecimal(b);returndecimal1.divide(decimal2,DFT_SCALE,roundingMode);}/***BigDecimal相加,四捨五入保留兩位**@parama*@paramb*@returna+b*/publicstaticBigDecimalsum(Stringa,Stringb,introundingMode){BigDecimaldecimal1=newBigDecimal(a);BigDecimaldecimal2=newBigDecimal(b);//DecimalFormatformat=newDecimalFormat("#0.00");returndecimal1.add(decimal2).setScale(DFT_SCALE,roundingMode);}/***BigDecimal相減,四捨五入保留兩位**@parama*@paramb*@returna+b*/publicstaticBigDecimalsub(Stringa,Stringb,introundingMode){BigDecimaldecimal1=newBigDecimal(a);BigDecimaldecimal2=newBigDecimal(b);//DecimalFormatformat=newDecimalFormat("#0.00");returndecimal1.subtract(decimal2).setScale(DFT_SCALE,roundingMode);}/***100.00為10000**@parama*@return*/publicstaticBigDecimalformat(Stringa,introundingMode){returnnewBigDecimal(a).multiply(newBigDecimal(100)).setScale(0,roundingMode);}publicstaticvoidmain(String[]args){Stringnumber="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方法是具體的調用。