Convert amount numbers in Java to uppercase numbers

Source: Internet
Author: User

Note: The code comes from someone else, and I introduced it and modified several bugs inside it. Now in my blog, so that it can be viewed later, and for other Bo friends to provide convenience

Modify the Bug Place: 1. The amount is 0 o'clock, does not add to the whole. It used to be 0 yuan, now it is 0 Yuan 2. Modify "points", "angle", "Yuan", "million", "billion", "mega" bit will not appear 0

Package com.zbx.bs.common.util;
Import Java.math.BigDecimal;

/**

* Digital conversion to capital <br> of Chinese RMB

*

* @author Dean

*/

public Class Numbertocn {

/**
* Digital capitalization in Chinese
*/
private static final string[] Cn_upper_number = {"0", "one", "II", "three", "the", "WU", "Lu", "Qi", "ba", "JIU"};
/**
* Chinese currency unit capital, this design is similar to the placeholder
*/
private static final string[] Cn_upper_monetray_unit = {"Min", "angle", "Yuan", "Pick", "Bai", "Qian", "Million", "Pick", "Bai", "Qian", "billion", "pick",
"Bai", "thousand", "trillion", "Pick Up", "Bai", "Thousand"};
/**
* Special characters: whole
*/
private static final String Cn_full = "whole";
/**
* Special characters: Negative
*/
private static final String cn_negative = "negative";
/**
* The precision of the amount, the default value is 2
*/
private static final int money_precision = 2;
/**
* Special characters: 0 Yuan Whole
*/
private static final String Cn_zeor_full = "0 Yuan";

/**
* Convert the amount entered into the capital of RMB in Chinese
*
* @param Numberofmoney
* The amount entered
* @return the corresponding Chinese capital
*/
public static String Number2cnmontrayunit (BigDecimal Numberofmoney) {
StringBuffer sb = new StringBuffer ();
Returns-1: Indicates that the number is less than 0 0: Indicates that the number equals 0 1: Indicates that the number is greater than 0
int signum = Numberofmoney.signum ();
0-Dollar situation
if (Signum = = 0) {
return cn_zeor_full;
}
The amount will be rounded up here.
Long number = Numberofmoney.movepointright (money_precision). Setscale (0, 4). ABS (). Longvalue ();
Get the two-bit value after the decimal point
Long scale = number% 100;
int numunit = 0;
int numindex = 0;
Boolean Getzero = false;
Judging the last two digits, there are four things: 00 = 0, 01 = 1, 10, 11
if (! ( Scale > 0)) {
Numindex = 2;
Number = number/100;
Getzero = true;
}
if (Scale > 0) && (! ( Scale% > 0)) {
Numindex = 1;
Number = NUMBER/10;
Getzero = true;
}
int zerosize = 0;
while (true) {
if (number <= 0) {
Break
}
Every time you get to the last number
Numunit = (int) (number% 10);
if (Numunit > 0) {
if ((Numindex = = 9) && (zerosize >= 3)) {
Sb.insert (0, cn_upper_monetray_unit[6]);
}
if ((Numindex = =) && (zerosize >= 3)) {
Sb.insert (0, cn_upper_monetray_unit[10]);
}
Sb.insert (0, Cn_upper_monetray_unit[numindex]);
Sb.insert (0, Cn_upper_number[numunit]);
Getzero = false;
zerosize = 0;
} else {
++zerosize;
"Min", "angle", "Yuan", "million", "billion", "mega" bit will not appear 0
if (Numindex! = 0 && Numindex! = 1 && numindex! = 2
&& Numindex! = 6 && Numindex! = && Numindex! = 14) {
if (! ( Getzero)) {
Sb.insert (0, Cn_upper_number[numunit]);
}
}

if (Numindex = = 2) {
if (number > 0) {
Sb.insert (0, Cn_upper_monetray_unit[numindex]);
}
} else if (((numIndex-2)% 4 = = 0) && (number% > 0)) {
Sb.insert (0, Cn_upper_monetray_unit[numindex]);
}
Getzero = true;
}
Let number remove the last digit every time
Number = NUMBER/10;
++numindex;
}
If Signum = =-1, the input number is negative, appending the special character to the front: negative
if (Signum = =-1) {
Sb.insert (0, cn_negative);

}//Except for 0.00 Other data should be with special characters: whole

Sb.append (Cn_full);
return sb.tostring ();
}

public static void Main (string[] args) {
Double money = 8880044841.10;
BigDecimal Numberofmoney = new BigDecimal (money);
SYSTEM.OUT.PRINTLN ("The amount you entered is:" "+ Money +" "#--# [" + Numbertocn.number2cnmontrayunit (Numberofmoney). toString () + "]");

}
}

Convert amount numbers in Java to uppercase numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.