Java implements two classes for converting Renminbi numbers into uppercase letters

Source: Internet
Author: User

To convert a Renminbi number into a Chinese character, we only need to use a relatively fixed method, that is, write the numbers in uppercase 1-10, and then store them in an array before comparison and conversion, the method does not have any advanced algorithms. It is very simple. For more information, see.

Method 1: Convert RMB numbers into Chinese capital prices using java

The Code is as follows: Copy code


Package xwcms.net. service;
Import java. util. HashMap;
/**
* Convert the numeric amount to the Chinese capital amount
* @ Author ftpeng
*
*/
Public class test {
/**
* RMB capital Unit System
*/
Private static HashMap <Integer, String> dws;
/**
* Numbers correspond to Chinese Characters
*/
Private static String [] jes;
// Initialization execution
Static {
Dws = new HashMap <Integer, String> ();
Dws. put (-2, "points ");
Dws. put (-1, "");
Dws. put (0, "Yuan ");
Dws. put (1, "pick up ");
Dws. put (2, "success ");
Dws. put (3, "success ");
Dws. put (4, "Ten Thousand ");//
Dws. put (5, "Pick ");
Dws. put (6, "success ");
Dws. put (7, "success ");
Dws. put (8, "");//
Dws. put (9, "Pick ");
Dws. put (10, "success ");
Dws. put (11, "success ");
Dws. put (12, "Ten Thousand ");
Jes = new String [] {"zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "identifier", "identifier "};
}
/**
* Convert numbers into uppercase letters
* @ Param number the number does not support scientific numbers
* @ Return
*/
Public static String chinese (String number ){
StringBuffer su = new StringBuffer ();
// Integer part
Number = delInvalidZero (number );
String str = null;
// Decimal part
String decimal = null;
If (number. contains (".")){
// Truncate an integer
Str = number. split ("\.") [0];
Decimal = number. split ("\.") [1];
} Else {
Str = number;
}
// Determine whether an integer exists
If (str. length ()> 0 ){
For (int I = 0; I <str. length (); I ++ ){
String context = str. substring (I, I + 1 );
Int pow = str. length ()-i-1;
Integer val = Integer. parseInt (context. toString ());
// Obtain the Chinese Unit
String sign = dws. get (pow );
// Obtain Chinese numbers
String name = jes [Integer. parseInt (context)];
If (val = 0 ){
If (pow % 4! = 0) {// Delete Unit
Sign = "";
}
If (I <str. length ()-1 ){
Integer val1 = Integer. parseInt (str. substring (I + 1, I + 2 ));
If (val = 0 & val = val1 ){
Name = "";
}
} Else if (I = str. length ()-1 ){
Name = "";
}
}
Su. append (name + sign );
}
}
// Determine whether there are decimal places
If (decimal! = Null ){
Str = decimal. substring (0, 1 );
If (! "0". equals (str )){
Su. append (jes [Integer. parseInt (str)] + dws. get (-1 ));
}
If (decimal. length () = 2 ){
Str = decimal. substring (1, 2 );
If (! "0". equals (str )){
Su. append (jes [Integer. parseInt (str)] + dws. get (-2 ));
}
}
} Else {
Su. append ("whole ");
}
Return su. toString ();
}
/**
* Clear special numeric characters
* @ Param str
* @ Return
*/
Private static String delInvalidZero (String str ){
If ("0". equals (str. substring (0, 1 ))){
Return delInvalidZero (str. substring (1, str. length ()));
} Else if (str. contains (",")){
Return delInvalidZero (str. replaceAll (",",""));
} Else {
Return str;
}
}
Public static void main (String [] args ){
System. out. println (test. chinese ("12.5 "));
}
}

Method 2,

 

The Code is as follows: Copy code

Package test;


Import java. text. DecimalFormat;


Public class Test {
Private static final String digit_0 = "";
Private static final String digit_1 = "0 August 5, 1234 ";
Private static final String [] digit_2 = {"", "10", "hundreds", "thousands "};
Private static final String [] digit_3 = {"", "Pick", "success", "success "};
Private static final String [] digit_4 = {"", "trillion "};


/**
* @ Param args
*/
Public static void main (String [] args ){
System. out. println (changeDigit ("2012.12", false ));
System. out. println (changeDigit ("210.12", true ));
System. out. println (changeNumberRMB (210.12 ));
}


/**
* Description: converts a number to an integer.
* @ Param str
* @ Param bo
* @ Return
*/
Public static String changeDigit (String str, boolean bo ){
StringBuffer strbu = new StringBuffer ();
Int dou = str. indexOf (".");
//: Determines whether it is a decimal or an integer. The length less than zero is an integer.
If (dou <0 ){
Dou = str. length ();
}
//: Obtain the integer part.
String inter = str. substring (0, dou );
Strbu. append (changeInteger (Long. parseLong (inter), bo ));
//: Process the fractional part.
If (dou! = Str. length ()){
Strbu. append ("point ");
//: Returns the number of decimal places.
String xh = str. substring (dou + 1 );
For (int I = 0; I <xh. length (); I ++ ){
If (bo ){
Strbu. append (digit_0.charAt (Integer. parseInt (xh. substring (
I, I + 1 ))));
} Else {
Strbu. append (digit_1.charAt (Integer. parseInt (xh. substring (
I, I + 1 ))));
}
}
}
String strs = strbu. toString ();
// Handle special cases, which may be incomplete
If (strs. startsWith ("0 ")){
Strs = strs. substring (1 );
}
If (strs. startsWith ("10 ")){
Strs = strs. substring (1 );
}
While (strs. endsWith ("0 ")){
Strs = strs. substring (0, strs. length ()-1 );
}
If (strs. startsWith ("point ")){
Strs = "zero" + strs;
}
If (strs. endsWith ("point ")){
Strs = strs. substring (0, strs. length ()-1 );
}
Return strs;
}


/**
* When the number of digits is less than 4, data processing is called.
*
* @ Param str
* @ Param bo
* @ Return
*/
Public static String readNumber (String str, boolean bo ){
StringBuffer strbu = new StringBuffer ();
If (str. length ()! = 4 ){
Return null;
}
For (int I = 0; I <4; I ++ ){
Char ch = str. charAt (I );
If (ch = '0' & I> 1 & str. charAt (I-1) = '0 '){
Continue;
}
If (ch! = '0' & I> 1 & str. charAt (I-1) = '0 '){
Strbu. append ('0 ');
}
If (ch! = '0 '){
If (bo ){
Strbu. append (digit_0.charAt (ch-48 ));
Strbu. append (digit_3 [4-I-1]);
} Else {
Strbu. append (digit_1.charAt (ch-48 ));
Strbu. append (digit_2 [4-I-1]);
}
}
}
Return strbu. toString ();
}


/**
* The integer part is converted to uppercase.
*
* @ Param lon
* @ Param bo
* @ Return
*/
Public static String changeInteger (long lon, boolean bo ){
StringBuffer strbu = new StringBuffer ();
//: Increase the number of digits by 3.
String strN = "000" + lon;
Int strN_L = strN. length ()/4;
//: Removes the number of strN "0" based on the length of different digits.
StrN = strN. substring (strN. length ()-strN_L * 4 );
For (int I = 0; I <strN_L; I ++ ){
String s1 = strN. substring (I * 4, I * 4 + 4 );
String s2 = readNumber (s1, bo );
Strbu. append (s2 );
If (s2.length ()! = 0 ){
Strbu. append (digit_4 [strN_L-I-1]);
}
}
String s = new String (strbu );
If (s. length ()! = 0 & s. startsWith ("0 "))
S = s. substring (1 );
Return s;
}


/**
* Name currency output format
*
* @ Param RMB
* @ Return
*/
Public static String changeNumberRMB (double RMB ){
String strRMB = "" + RMB;
DecimalFormat df = new DecimalFormat ("#.#");
Df. setMaximumFractionDigits (2 );
StrRMB = df. format (RMB). toString ();
//: True in uppercase and return data
StrRMB = changeDigit (strRMB, true );
If (strRMB. indexOf ("point")> = 0 ){
StrRMB = strRMB + "zero ";
StrRMB = strRMB. replaceAll ("point", "circle ");
String str1 = strRMB. substring (0, strRMB. indexOf ("circle") + 1 );
String str2 = strRMB. substring (strRMB. indexOf ("circle") + 1 );
StrRMB = str1 + str2.charAt (0) + "" + str2.charAt (1) + "";
} Else {
StrRMB = strRMB + "round ";
}
Return "RMB (uppercase):" + strRMB;
}


}

Related Article

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.