導航:首頁 > 編程語言 > java數字大小寫

java數字大小寫

發布時間:2023-06-18 19:01:08

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如何將數字轉為中文大寫

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 數字轉換為中文大寫的轉換

package com.heyang;

/**
* 將10億以內的阿拉伯數字轉成漢字大寫形式
* @author xizhenyin
*
*/
public class CnUpperCaser {
// 整數部分
private String integerPart;
// 小數部分
private String floatPart;

// 將數字轉化為漢字的數組,因為各個實例都要使用所以設為靜態
private static final char[] cnNumbers={'零','壹','貳','叄','肆','伍','陸','柒','捌','玖'};

// 供分級轉化的數組,因為各個實例都要使用所以設為靜態
private static final char[] series={'元','拾','百','仟','萬','拾','百','仟','億'};

/**
* 構造函數,通過它將阿拉伯數字形式的字元串傳入
* @param original
*/
public CnUpperCaser(String original){
// 成員變數初始化
integerPart="";
floatPart="";

if(original.contains(".")){
// 如果包含小數點
int dotIndex=original.indexOf(".");
integerPart=original.substring(0,dotIndex);
floatPart=original.substring(dotIndex+1);
}
else{
// 不包含小數點
integerPart=original;
}
}

/**
* 取得大寫形式的字元串
* @return
*/
public String getCnString(){
// 因為是累加所以用StringBuffer
StringBuffer sb=new StringBuffer();

// 整數部分處理
for(int i=0;i<integerPart.length();i++){
int number=getNumber(integerPart.charAt(i));

sb.append(cnNumbers[number]);
sb.append(series[integerPart.length()-1-i]);
}

// 小數部分處理
if(floatPart.length()>0){
sb.append("點");
for(int i=0;i<floatPart.length();i++){
int number=getNumber(floatPart.charAt(i));

sb.append(cnNumbers[number]);
}
}

// 返回拼接好的字元串
return sb.toString();
}

/**
* 將字元形式的數字轉化為整形數字
* 因為所有實例都要用到所以用靜態修飾
* @param c
* @return
*/
private static int getNumber(char c){
String str=String.valueOf(c);
return Integer.parseInt(str);
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new CnUpperCaser("123456789.12345").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
System.out.println(new CnUpperCaser(".123456789").getCnString());
System.out.println(new CnUpperCaser("0.1234").getCnString());
System.out.println(new CnUpperCaser("1").getCnString());
System.out.println(new CnUpperCaser("12").getCnString());
System.out.println(new CnUpperCaser("123").getCnString());
System.out.println(new CnUpperCaser("1234").getCnString());
System.out.println(new CnUpperCaser("12345").getCnString());
System.out.println(new CnUpperCaser("123456").getCnString());
System.out.println(new CnUpperCaser("1234567").getCnString());
System.out.println(new CnUpperCaser("12345678").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
}
}

Ⅳ java 數字轉大寫漢字

/**只貼修改的formatTo(int money)方法這一部分了,希望有幫助*/
public void formatTo(int money) {
Map<Integer, String> moneyMap = new HashMap<Integer, String>();
Map<Integer, String> moneyMap2 = new HashMap<Integer, String>();
StringBuffer sb = new StringBuffer();
moneyMap.put(1, "壹");
moneyMap.put(2, "貳");
moneyMap.put(3, "叄");
moneyMap.put(4, "肆");
moneyMap.put(5, "伍");
moneyMap.put(6, "陸");
moneyMap.put(7, "柒");
moneyMap.put(8, "捌");
moneyMap.put(9, "玖");
moneyMap.put(0, "零"); //
moneyMap2.put(1, null);
moneyMap2.put(2, "拾");
moneyMap2.put(3, "佰");
moneyMap2.put(4, "仟");
moneyMap2.put(5, "萬");
moneyMap2.put(6, "十");
moneyMap2.put(7, "百");
moneyMap2.put(8, "千");
moneyMap2.put(9, "億");
moneyMap2.put(10, "十");
moneyMap2.put(11, "百");
moneyMap2.put(12, "千");
moneyMap2.put(13, "萬");
String moneyString = (money + "").trim().toString();
int moneyLength = moneyString.length();
num = moneyLength;
//標記上一層是否為零
boolean isPrevNumberZero = false;
for (int i = 0; i < moneyLength; i++) {
Character c = moneyString.charAt(i);
Integer key = Character.getNumericValue(c);
// 如果是0 跳過,並把isPrevNumberZero設為true
if(key == 0 ){
isPrevNumberZero = true;
num--;
continue;
}
//如果上層是0,則sb字元串增加一個零
if(isPrevNumberZero){
sb.append(moneyMap.get(0));
isPrevNumberZero = false;
}
if (i < moneyLength - 1) {
sb.append(moneyMap.get(key)).append(moneyMap2.get(num));
num--;
}
if (i == moneyLength - 1 && num == 1) {
sb.append(moneyMap.get(key)).append("元整");
}
}
System.out.println(sb);
}

Ⅳ 用JAVA如何把小寫數字變成大寫

我以前用的人名幣大小寫轉換,你改改應該就可以了:
public class Test6 {
static final String zhnum_0 = "零壹貳叄肆伍陸柒捌玖";

static final String zhnum = "零一二三四五六七八九";

static final String[] zhnum1 = { "", "十", "百", "千" };

static final String[] zhnum1_0 = { "", "拾", "佰", "仟" };

static final String[] zhnum2 = { "", "萬", "億", "萬億", "億億" };

public Test6() {
}

private static String numberToZH4(String s, boolean fan) {
StringBuffer sb = new StringBuffer();
if (s.length() != 4)
return null;
for (int i = 0; i < 4; i++) {
char c1 = s.charAt(i);
if (c1 == '0' && i > 1 && s.charAt(i - 1) == '0')
continue;
if (c1 != '0' && i > 1 && s.charAt(i - 1) == '0')
sb.append('零');
if (c1 != '0') {
if (fan) {
sb.append(zhnum_0.charAt(c1 - 48));
sb.append(zhnum1_0[4 - i - 1]);
} else {
sb.append(zhnum.charAt(c1 - 48));
sb.append(zhnum1[4 - i - 1]);
}
}
}
return new String(sb);
}

public static String numberToZH(long n, boolean fan) {
StringBuffer sb = new StringBuffer();
String strN = "000" + n;
int strN_L = strN.length() / 4;
strN = strN.substring(strN.length() - strN_L * 4);
for (int i = 0; i < strN_L; i++) {
String s1 = strN.substring(i * 4, i * 4 + 4);
String s2 = numberToZH4(s1, fan);
sb.append(s2);
if (s2.length() != 0)
sb.append(zhnum2[strN_L - i - 1]);
}
String s = new String(sb);
if (s.length() != 0 && s.startsWith("零"))
s = s.substring(1);
return s;
}

public static String numberToZH(double d, boolean fan) {
return numberToZH("" + d, fan);
}

public static String numberToZH(String str, boolean fan) {
StringBuffer sb = new StringBuffer();
int dot = str.indexOf(".");
if (dot < 0)
dot = str.length();

String zhengshu = str.substring(0, dot);
sb.append(numberToZH(Long.parseLong(zhengshu), fan));
if (dot != str.length()) {
sb.append("點");
String xiaoshu = str.substring(dot + 1);
for (int i = 0; i < xiaoshu.length(); i++) {
if (fan) {
sb.append(zhnum_0.charAt(Integer.parseInt(xiaoshu
.substring(i, i + 1))));
} else {
sb.append(zhnum.charAt(Integer.parseInt(xiaoshu.substring(
i, i + 1))));
}
}
}
String s = new String(sb);
if (s.startsWith("零"))
s = s.substring(1);
if (s.startsWith("一十"))
s = s.substring(1);
while (s.endsWith("零")) {
s = s.substring(0, s.length() - 1);
}
if (s.endsWith("點"))
s = s.substring(0, s.length() - 1);
return s;
}

public static String numberToRMB(double rmb) {
String strRMB = "" + rmb;
DecimalFormat nf = new DecimalFormat("#.#");
nf.setMaximumFractionDigits(2);
strRMB = nf.format(rmb).toString();
strRMB = numberToZH(strRMB, true);
if (strRMB.indexOf("點") >= 0) {
strRMB = strRMB + "零";
strRMB = strRMB.replaceAll("點", "圓");
String s1 = strRMB.substring(0, strRMB.indexOf("圓") + 1);
String s2 = strRMB.substring(strRMB.indexOf("圓") + 1);
strRMB = s1 + s2.charAt(0) + "角" + s2.charAt(1) + "分整";
} else {
strRMB = strRMB + "圓整";
}
return "人民幣(大寫):" + strRMB;
}

public static void main(String[] args) {
System.out.println(numberToRMB(342345.96));
System.out.println(numberToRMB(123));
}
}

閱讀全文

與java數字大小寫相關的資料

熱點內容
java帶參數的線程 瀏覽:913
為什麼安卓車載中控屏看起來很差 瀏覽:466
吃雞怎麼解壓最快 瀏覽:968
linux網路編程基礎 瀏覽:219
產研是程序員嗎 瀏覽:594
程序員的法律 瀏覽:969
編程第四關用冰雪火焰閃現通關 瀏覽:756
批處理當前文件夾參數 瀏覽:185
鴻蒙安卓如何下載 瀏覽:904
開3389命令 瀏覽:542
程序員大都單純嗎 瀏覽:915
APP如何實現下載功能 瀏覽:216
通達信源碼怎樣放到桌面 瀏覽:645
程序員的腦袋會禿嗎 瀏覽:455
為什麼eve登錄啟動不進去伺服器 瀏覽:272
微信招生app哪個好用 瀏覽:233
寶可夢劍盾啟動文件在哪個文件夾 瀏覽:765
壓縮機比容 瀏覽:117
python自動化測試面試 瀏覽:949
買便宜點的鞋子去哪個app買 瀏覽:890