本文目錄一覽:
- 1、Java將控制台輸入的人民幣數字金額轉化為大寫
- 2、用java編譯金額的中文大寫轉換。
- 3、怎樣用Java將金額轉換為中文大寫形式
- 4、Java金額的中文大寫方式
- 5、java如何將數字轉為中文大寫
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編譯金額的中文大寫轉換。
/**
* 金額小數轉換成中文大寫金額
* @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將金額轉換為中文大寫形式
String words=”12334″;
for(int i=0 ; iwords.length ; i++)
{
switch(words.charAt(i))
{
case “1”:System.out.println(“壹”);break;
case “2”:
…………..
case “0”:System.out.println(“零”);break;
}
}
大致思路是這個樣子的吧。
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 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.length1 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;inumber.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; ilength; 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;idecimals.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));
}
}
原創文章,作者:TIOHA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/325220.html