您的位置:

java将金额转成中文(java将金额转换为大写人民币)

本文目录一览:

JAVA如何把键盘输入的金额换转成中文显示。在线求解释~~~

/**

 * 将10亿以内的阿拉伯数字转成汉字大写形式

 * @author Hp

 * 

 */

public class TestNum {

  // 整数部分

  private String integerPart;

  // 小数部分

  private String floatPart;

  

  // 将数字转化为汉字的数组,因为各个实例都要使用所以设为静态

  private static final char[] cnNumbers={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};

  

  // 供分级转化的数组,因为各个实例都要使用所以设为静态

  private static final char[] series={'元','拾','百','仟','万','拾','百','仟','亿'};

  

  /**

   * 构造函数,通过它将阿拉伯数字形式的字符串传入

   * @param original

   */

  public TestNum(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;iintegerPart.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;ifloatPart.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 TestNum("123456789.12345").getCnString());

    System.out.println(new TestNum("123456789").getCnString());

    System.out.println(new TestNum(".123456789").getCnString());

    System.out.println(new TestNum("0.1234").getCnString());

    System.out.println(new TestNum("1").getCnString());

    System.out.println(new TestNum("12").getCnString());

    System.out.println(new TestNum("123").getCnString());

    System.out.println(new TestNum("1234").getCnString());

    System.out.println(new TestNum("12345").getCnString());

    System.out.println(new TestNum("123456").getCnString());

    System.out.println(new TestNum("1234567").getCnString());

    System.out.println(new TestNum("12345678").getCnString());

    System.out.println(new TestNum("123456789").getCnString());

  }

}

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));

}

}

用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));

}

}