溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java金額數(shù)字轉(zhuǎn)中文工具類詳解

發(fā)布時(shí)間:2020-09-18 20:02:25 來源:腳本之家 閱讀:138 作者:阿杜_ardo 欄目:編程語言

本文實(shí)例為大家分享了java金額數(shù)字轉(zhuǎn)中文工具類的具體代碼,供大家參考,具體內(nèi)容如下

java金額數(shù)字轉(zhuǎn)中文工具類ConvertNum.java

package light.mvc.utils;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 金額數(shù)字轉(zhuǎn)中文工具類
 * 
 * @author ardo
 * 
 */
public class ConvertNum {
 
 /**
 * 把金額阿拉伯?dāng)?shù)字轉(zhuǎn)換為漢字表示,小數(shù)點(diǎn)后四舍五入保留兩位
 * 還有一種方法可以在轉(zhuǎn)換的過程中不考慮連續(xù)0的情況,然后對最終的結(jié)果進(jìn)行一次遍歷合并連續(xù)的零
 */
 public static String[] ChineseNum = new String[] { "零", "壹", "貳", "叁", "肆",
  "伍", "陸", "柒", "捌", "玖" };
 
 public static String NumToChinese(double num) {
 if (num > 99999999999999.99 || num < -99999999999999.99)
  throw new IllegalArgumentException(
   "參數(shù)值超出允許范圍 (-99999999999999.99 ~ 99999999999999.99)!");
 boolean negative = false;// 正負(fù)標(biāo)號
 if (num < 0) {
  negative = true;
  num = num * (-1);
 }
 long temp = Math.round(num * 100);
 int numFen = (int) (temp % 10);// 分
 temp = temp / 10;
 int numJiao = (int) (temp % 10);// 角
 temp = temp / 10;
 // 此時(shí)temp只包含整數(shù)部分
 int[] parts = new int[20];// 將金額整數(shù)部分分為在0-9999之間數(shù)的各個(gè)部分
 int numParts = 0;// 記錄把原來金額整數(shù)部分分割為幾個(gè)部分
 for (int i = 0;; i++) {
  if (temp == 0)
  break;
  int part = (int) (temp % 10000);
  parts[i] = part;
  temp = temp / 10000;
  numParts++;
 }
 boolean beforeWanIsZero = true;// 標(biāo)志位,記錄萬的下一級是否為0
 String chineseStr = "";
 for (int i = 0; i < numParts; i++) {
  String partChinese = partConvert(parts[i]);
  if (i % 2 == 0) {
  if ("".equals(partChinese))
   beforeWanIsZero = true;
  else
   beforeWanIsZero = false;
  }
  if (i != 0) {
  if (i % 2 == 0)// 億的部分
   chineseStr = "億" + chineseStr;
  else {
   if ("".equals(partChinese) && !beforeWanIsZero)// 如果“萬”對應(yīng)的
         // part 為
         // 0,而“萬”下面一級不為
         // 0,則不加“萬”,而加“零”
   chineseStr = "零" + chineseStr;
   else {
   if (parts[i - 1] < 1000 && parts[i - 1] > 0)// 如果萬的部分不為0,而萬前面的部分小于1000大于0,則萬后面應(yīng)該跟零
    chineseStr = "零" + chineseStr;
   chineseStr = "萬" + chineseStr;
   }
  }
  }
  chineseStr = partChinese + chineseStr;
 }
 if ("".equals(chineseStr))// 整數(shù)部分為0,則表示為零元
  chineseStr = ChineseNum[0];
 else if (negative)// 整數(shù)部分部位0,但是為負(fù)數(shù)
  chineseStr = "負(fù)" + chineseStr;
 chineseStr = chineseStr + "元";
 if (numFen == 0 && numJiao == 0) {
  chineseStr = chineseStr + "整";
 } else if (numFen == 0) {// 0分
  chineseStr = chineseStr + ChineseNum[numJiao] + "角";
 } else {
  if (numJiao == 0)
  chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
  else
  chineseStr = chineseStr + ChineseNum[numJiao] + "角"
   + ChineseNum[numFen] + "分";
 }
 return chineseStr;
 }
 
 // 轉(zhuǎn)換拆分后的每個(gè)部分,0-9999之間
 public static String partConvert(int partNum) {
 if (partNum < 0 || partNum > 10000) {
  throw new IllegalArgumentException("參數(shù)必須是大于等于0或小于10000的整數(shù)");
 }
 String[] units = new String[] { "", "拾", "佰", "仟" };
 int temp = partNum;
 String partResult = new Integer(partNum).toString();
 int partResultLength = partResult.length();
 boolean lastIsZero = true;// 記錄上一位是否為0
 String chineseStr = "";
 for (int i = 0; i < partResultLength; i++) {
  if (temp == 0)// 高位無數(shù)字
  break;
  int digit = temp % 10;
  if (digit == 0) {
  if (!lastIsZero)// 如果前一個(gè)數(shù)字不是0則在當(dāng)前漢字串前加零
   chineseStr = "零" + chineseStr;
  lastIsZero = true;
  } else {
  chineseStr = ChineseNum[digit] + units[i] + chineseStr;
  lastIsZero = false;
  }
  temp = temp / 10;
 }
 return chineseStr;
 }
 
 public static void main(String args[]) {
 double num = 0;
 System.out.println("請輸入金額數(shù)字,-1退出");
 try {
  BufferedReader br = new BufferedReader(new InputStreamReader(
   System.in));
  num = Double.parseDouble(br.readLine());
 } catch (IOException e) {
  System.out.println(e.toString());
 }
 while (num != -1) {
  System.out.println(num + NumToChinese(num));
  try {
  BufferedReader br = new BufferedReader(new InputStreamReader(
   System.in));
  num = Double.parseDouble(br.readLine());
  } catch (IOException e) {
  System.out.println(e.toString());
  }
 }
 System.out.println("其他測試:");
 System.out.println("100120: " + NumToChinese(100120));
 System.out.println("25000000000005.999: "
  + NumToChinese(25000000000005.999));
 System.out.println("45689263.626: " + NumToChinese(45689263.626));
 System.out.println("0.69457: " + NumToChinese(0.69457));
 System.out.println("253.0: " + NumToChinese(253.0));
 System.out.println("0: " + NumToChinese(0));
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI