java數字大寫
『壹』 java 數字轉大寫漢字
java 數字轉大寫漢字的完整例子,請參考:
package com.sitinspring;
import java.math.BigDecimal;
import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 將阿拉伯數字轉換成漢語大寫數字
* @author sitinspring
* @date 2008-03-25
*/
public class ChineseUpperCaser{
/**
* 用於存儲整數部分
*/
private String integerPart;
/**
* 用於存儲小數部分
*/
private String floatPart;
/**
* 用於存儲0-9大寫的哈希表
*/
private static final Map<String,String> ZerotoNineHt;
/**
* 用於存儲十百千大寫的哈希表
*/
private static final Map<Integer,String> thHuTenHt;
/**
* 用於存儲萬億兆大寫的哈希表
*/
private static final Map<Integer,String> wanYiZhaoHt;
static{
ZerotoNineHt=new Hashtable<String,String>();
ZerotoNineHt.put("0", "零");
ZerotoNineHt.put("1", "壹");
ZerotoNineHt.put("2", "貳");
ZerotoNineHt.put("3", "叄");
ZerotoNineHt.put("4", "肆");
ZerotoNineHt.put("5", "伍");
ZerotoNineHt.put("6", "陸");
ZerotoNineHt.put("7", "柒");
ZerotoNineHt.put("8", "捌");
ZerotoNineHt.put("9", "玖");
thHuTenHt=new Hashtable<Integer,String>();
thHuTenHt.put(0, "");
thHuTenHt.put(1, "拾");
thHuTenHt.put(2, "佰");
thHuTenHt.put(3, "仟");
wanYiZhaoHt=new Hashtable<Integer,String>();
wanYiZhaoHt.put(0, "");
wanYiZhaoHt.put(1, "萬");
wanYiZhaoHt.put(2, "億");
wanYiZhaoHt.put(3, "兆");
}
private static String getWanYiZhao(int level){
String retval="";
do{
retval+=wanYiZhaoHt.get(level % 4);
level-=3;
}while(level>3);
return retval;
}
/**
* 構造函數
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(float number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 構造函數
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(double number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 構造函數
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(int number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 構造函數
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(long number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 構造函數
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(String number) throws NumberFormatException{
String formalNumber=formatNumber(number);
// 辟分以給整數部分和小數部分賦值
String[] arr=formalNumber.split("[.]");
if(arr.length==2){
// 有小數點
integerPart=arr[0];
floatPart=arr[1];
}
else{
// 無小數點
integerPart=arr[0];
}
}
public String toString(){
String retval="";
if(integerPart!=null){
retval+=parseIntegerPart();
}
if(floatPart!=null){
retval+=parseFloatPart();
}
else{
retval+="整";
}
return retval;
}
/**
* 得到整數部分的漢字大寫表示
* @return
*/
private String parseIntegerPart(){
String retval="";
// 將整數部分逆序,因為需要反向讀取
String reverseIntegerPart="";
for(int i=integerPart.length()-1;i>-1;i--){
reverseIntegerPart+=integerPart.charAt(i);
}
// 將整數部分按四位分段
Pattern p = Pattern.compile("\\d{4}",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(reverseIntegerPart);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while (result) {
// 每找到四位放一個逗號
m.appendReplacement(sb, m.group(0) + ",");
result = m.find();
}
m.appendTail(sb);
// 按逗號劈分,得到四位分組數據的數組
String[] arr=sb.toString().split(",");
int j;
String str;
for(int i=arr.length-1;i>=0;i--){
String temp=arr[i];
// 阿拉伯數字轉大寫漢字加單位(千百十)
for(j=temp.length()-1;j>=0;j--){
str=String.valueOf(temp.charAt(j));
retval+=ZerotoNineHt.get(str)+thHuTenHt.get(j);
}
retval=retval.replaceAll("(零)($)", "$2");// 零在末尾則去掉
// 加單位(兆億萬)
retval+=getWanYiZhao(i);
}
// 零替換
retval=retval.replaceAll("(零[仟佰拾])", "零");
retval=retval.replaceAll("(零{2,})", "零");
retval=retval.replaceAll("(零)($)", "$2");// 零在末尾則去掉
return retval;
}
/**
* 得到小數部分的漢字大寫表示
* @return
*/
private String parseFloatPart(){
String retval="點";
for(int i=0;i<floatPart.length();i++){
String temp=String.valueOf(floatPart.charAt(i));
retval+=ZerotoNineHt.get(temp);
}
return retval;
}
/**
* 對輸入的字元串進行驗證,如果不能轉化為數字形式則拋出數字轉化異常
* ,注意這是一個運行時異常(非檢查型異常),程序不用顯式捕獲
* @param number
* @throws NumberFormatException
*/
private String formatNumber(String number) throws NumberFormatException{
return (new BigDecimal(number)).toString();
}
public static void main(String[] args){
String[] arr={"1.543524304302432","12.432423432","123.454235","1234","12345","123456","1234567",
"12345678","123456789","1234567891","12345678912","123456789123","1234567891234","12345678912345",
"123456789123456","1234567891234567","12345678912345678","123456789123456789",
"","0","00","000","0000","01","001","0001",
"00001","10","100","1000","10000","101","1001","10001","100001","1.23","21.234","243400031.233234",
"5400035.980","543.6545"};
//String[] arr={"0","00","000","0000","01","001","0001","00001","10","100","1000","10000","101","1001","10001","100001"};
//String[] arr={"1.23","21.234","243400031.233234","5400035.980","543.6545"};
for(String str:arr){
System.out.println("阿拉伯數字等於:"+str+" 大寫漢字等於:"+new ChineseUpperCaser(str));
}
}
}
『貳』 java數字大小寫轉換
給你段代碼參考:
public class NumToChinese {
public static String NumberToChinese(String input){
String s1="零壹貳叄肆伍陸柒捌玖";
String s4="分角整元拾佰仟萬拾佰仟億拾佰仟";
String temp="";
String result="";
if (input==null) return "輸入的字串不是數字串只能包括以下字元('0'~'9','.'),輸入字串最大隻能精確到仟億,小數點只能兩位!";
temp=input.trim();
float f;
try{
f=Float.parseFloat(temp);
}catch(Exception e){
return "輸入的字串不是數字串只能包括以下字元('0'~'9','.'),輸入字串最大隻能精確到仟億,小數點只能兩位!";
}
int len=0;
if(temp.indexOf(".")==-1) len=temp.length();
else len=temp.indexOf(".");
if(len>s4.length()-3) return("輸入字串最大隻能精確到仟億,小數點只能兩位!");
int n1=0;
String num="";
String unit="";
for(int i=0;i<temp.length();i++){
if(i>len+2){break;}
if(i==len) {continue;}
n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
num=s1.substring(n1,n1+1);
n1=len-i+2;
unit=s4.substring(n1,n1+1);
result=result.concat(num).concat(unit);
}
if((len==temp.length())||(len==temp.length()-1)) result=result.concat("整");
if(len==temp.length()-2) result=result.concat("零分");
return result;
}
}
『叄』 用JAVA編程將任意一個整數轉換成中文大寫,如101轉換為一百零一
import java.io.BufferedReader;
import java.io.FileReader;
public class setrs
{
public static void main(String[] args)
throws Exception
{
String fileName = "c:\\input.txt";
// 單位數組
String[] units = new String[] {"十", "百", "千", "萬", "十", "百", "千", "億"};
// 中文大寫數字數組
String[] numeric = new String[] {"零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};
// 讀文件
BufferedReader br = new BufferedReader(new FileReader(fileName));
String temp = null;
temp = br.readLine();
String res = "";
while (null != temp)
{
// 遍歷一行中所有數字
for (int k = -1; temp.length() > 0; k++)
{
// 解析最後一位
int j = Integer.parseInt(temp.substring(temp.length() - 1, temp.length()));
String rtemp = numeric[j];
// 數值不是0且不是個位 或者是萬位或者是億位 則去取單位
if (j != 0 && k != -1 || k % 8 == 3 || k % 8 == 7)
{
rtemp += units[k % 8];
}
// 拼在之前的前面
res = rtemp + res;
// 去除最後一位
temp = temp.substring(0, temp.length() - 1);
}
// 去除後面連續的零零..
while (res.endsWith(numeric[0]))
{
res = res.substring(0, res.lastIndexOf(numeric[0]));
}
// 將零零替換成零
while (res.indexOf(numeric[0] + numeric[0]) != -1)
{
res = res.replaceAll(numeric[0] + numeric[0], numeric[0]);
}
// 將 零+某個單位 這樣的竄替換成 該單位 去掉單位前面的零
for (int m = 1; m < units.length; m++)
{
res = res.replaceAll(numeric[0] + units[m], units[m]);
}
// 這里列印一下 可以改成寫文件
System.out.println(res);
// 讀取下一個數
res = "";
temp = br.readLine();
}
}
}
這代碼是別人寫的希望對你有幫助,祝你學習進步。
『肆』 用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怎麼將數字1轉換為大寫一
如下是一個將阿拉伯數字轉為漢字的類,需要者可收藏到自己的util類庫中以備不時之需。
public class ArabicToChineseUtils {
static String[] units = { "", "十", "百", "千", "萬", "十萬", "百萬", "千萬", "億", "十億", "百億", "千億", "萬億" };
static char[] numArray = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
/**
* @param args
*/
public static void main(String[] args) {
int num = 245000006;
String numStr = foematInteger(num);
print("num= " + num + ", convert result: " + numStr);
double decimal = 245006.234206;
print("============================================================");
String decStr = formatDecimal(decimal);
print("decimal= " + decimal + ", decStr: " + decStr);
}
private static String foematInteger(int num) {
char[] val = String.valueOf(num).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
String m = val[i] + "";
int n = Integer.valueOf(m);
boolean isZero = n == 0;
String unit = units[(len - 1) - i];
if (isZero) {
if ('0' == val[i - 1]) {
// not need process if the last digital bits is 0
continue;
} else {
// no unit for 0
sb.append(numArray[n]);
}
} else {
sb.append(numArray[n]);
sb.append(unit);
}
}
return sb.toString();
}
private static String formatDecimal(double decimal) {
String decimals = String.valueOf(decimal);
int decIndex = decimals.indexOf(".");
int integ = Integer.valueOf(decimals.substring(0, decIndex));
int dec = Integer.valueOf(decimals.substring(decIndex + 1));
String result = foematInteger(integ) + "." + formatFractionalPart(dec);
return result;
}
private static String formatFractionalPart(int decimal) {
char[] val = String.valueOf(decimal).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
int n = Integer.valueOf(val[i] + "");
sb.append(numArray[n]);
}
return sb.toString();
}
private static void print(Object arg0) {
System.out.println(arg0);
}
}
『陸』 JAVA練習題 金額的中文大寫
//我自己寫的
//把String 換成了StringBuilder 因為後者只要新建後,無論如何變化,都是同一個字元串,
//而String 是不可變的, 每改變一次,就要新建一個變數
//也可以用StringBuffer , 和StringBuilder用法一樣, 不過是線程安全的
public class SmallToBig {
/**
* @param args
* 把數字換成大寫的中文貨幣字元 123456789 換成
* 零,壹,貳,叄,肆,伍,陸,柒,捌,玖
* "元","十","佰","仟","萬","十","佰","仟","億","十","佰","仟"," ","","","" 太大的就沒寫了, 直接輸出大寫數字
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("3789341.512");
System.out.print( getFloatBigCn(sb));
}
public static StringBuilder getFloatBigCn(StringBuilder sb){
String [] big={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
String [] fUnit ={"角","分","","",""};
StringBuilder floatStr=new StringBuilder();
if(sb.indexOf(".")>-1){ //是否存在小數點
StringBuilder intPart = new StringBuilder(sb.substring(0, sb.indexOf(".")));//取得整數部分
StringBuilder flPart = new StringBuilder(sb.substring(sb.indexOf(".")+1, sb.length()));//取得小數部分
//整數部分處理
floatStr.append(getIntBigCn(intPart));//整數部分給整數方法處理
//小數點後處理
if(flPart.length()>2)
flPart = flPart.delete(2, flPart.length()); //小數點後只取到分 ,注意,此部會把2位後的數值刪除掉
for (int i = 0; i <= flPart.length()-1; i++) {
int t = Integer.parseInt(flPart.substring(i, i+1));
floatStr.append(big[t]+fUnit[i]); // 數值加單位
}
}else //如果沒有小數點, 直接給處理整數的方法處理
floatStr.append(getIntBigCn(sb));
return floatStr;
}
public static StringBuilder getIntBigCn(StringBuilder sb){
String[] big={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
String [] iUnit ={"元","十","佰","仟","萬","十","佰","仟","億",
"十","佰","仟"," ","","","","","","","",""};//中國人太有錢。。。超出單位所能承受
StringBuilder intStr=new StringBuilder();
int cnt=0;//換單位
for(int i=sb.length();i>0 ; i--){ //倒取的值
int m = Integer.parseInt(sb.substring(i-1,i));
intStr.insert(0,big[m]+iUnit[cnt]);//從前面插入
cnt++;
}
return intStr;
}
}
『柒』 如何用java編一個程序將輸入的阿拉伯數字轉換成大寫的漢字
import java.util.HashMap;
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(0, "零");
hashMap.put(1, "壹");
hashMap.put(2, "貳");
hashMap.put(3, "叄");
hashMap.put(4, "肆");
hashMap.put(5, "伍");
hashMap.put(6, "陸");
hashMap.put(7, "柒");
hashMap.put(8, "捌");
hashMap.put(9, "玖");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
for (int i = 0; i < input.length(); i++) {
System.out.print(hashMap.get(input.charAt(i) - 48));
}
}
}
