Java implementation Digital amount converted to Chinese capital amount

Source: Internet
Author: User

We often do the project's financial statements, need to print invoices and other situations need to use the lower case financial amount to convert the capital amount of Chinese characters, the following is the code to implement the function


Import Java.lang.Math;

/**
* General-purpose Money conversion class
*
*/
public class Money {
Private String handigistr[] = new string[] {"0", "one", "Ii.", "three", "the", "WU",
"Lu", "Qi", "ba", "JIU"};
Private String handivistr[] = new string[] {"", "Pick", "Bai", "Thousand", "Million", "pick",
"Bai", "Qian", "billion", "Pick Up", "Bai", "Qian", "Million", "Pick", "Bai", "Qian", "billion", "pick", "Bai",
"Thousand", "Million", "Pick", "Bai", "Thousand"};
/**
* @param numstr Input string must be a positive integer, only leading spaces (must be right justified), should not have leading zeros
* @return
*/
String Positiveintegertohanstr (String numstr) {
String rmbstr = "";
Boolean Lastzero = false;
Boolean hasvalue = false; MJ digits are marked before
int Len, n;
Len = Numstr.length ();
if (Len > 15)
Return "value too big!";
for (int i = len-1; I >= 0; i--) {
if (Numstr.charat (len-i-1) = = ")
Continue
n = Numstr.charat (Len-i-1)-' 0 ';
if (N < 0 | | n > 9)
Return "input with non-numeric characters!";

if (n! = 0) {
if (Lastzero)
Rmbstr + = handigistr[0]; Some 0 followed by a non-0 value, showing only one 0
Except for hundreds of billions of years ago, 0 didn't bring back.
if (!) (N==1 && (i%4) ==1 && (Lastzero | | i==len-1))//
If there are 0 before the decimal, do not send a tone with this line
if (! ( n = = 1 && (i% 4) = = 1 && i = = len-1))//10 carry in the first position does not send a sound
Rmbstr + = Handigistr[n];
Rmbstr + = Handivistr[i]; Non-0 value plus carry, single digit is empty
HasValue = true; Value tag before placing MJ bit

} else {
if ((i% 8) = = 0 | | ((i% 8) = = 4 && hasvalue)) Hundreds of billions must have a non-0 value to show million
Rmbstr + = Handivistr[i]; "Million" or "million"
}
if (i% 8 = = 0)
HasValue = false; MJ bit pre-value mark million reset
Lastzero = (n = = 0) && (i% 4! = 0);
}

if (rmbstr.length () = = 0)
return handigistr[0]; Enter a null character or "0" and return "0"
return rmbstr;

}


I am engaged in software project development 20, 10 Java Engineer Series of teaching work, recording more than 30 quality video courses, each course contains the project actual combat, class ppt, and complete source code download, interested friends can see my online classroom, If you want to learn more about the full Java course, take a look at my Java from getting started to mastering + Project Training video lessons: http://edu.51cto.com/course/6946.html


Java implementation Digital amount converted to Chinese capital amount

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.