Convert the numeric string RMB into a uppercase string RMB

Source: Internet
Author: User
/*
* Creation date: 2005-3-8
*
* To change the template of the generated file in todo, go
* Window-preferences-Java-code style-Code Template
*/
Package com. test;

/**
* @ Author Administrator
*
* To change the template of the type comment generated by todo, go
* Window-preferences-Java-code style-Code Template
*/
Public class testrmb {
/**
* Test the numberconvert method.
* @ Param ARGs string []
*/
Public static void main (string [] ARGs ){
// This is the largest number that can be converted
String RMB = "90909.99 ";
System. Out. println (numberconvert (RMB ));
}

/**
* Converts string numbers to Chinese characters.
* @ Param in number
* @ Return (uppercase)
*/
Public static string numberconvert (string in ){
Stringbuffer out = new stringbuffer ();
String CHN [] = {
"Zero", "one", "two", "San", "Si", "Wu", "Lu", "Wu ", "success "};
String end [] = {
"", "Pick", "success", "fail ",};
String mid [] = {
"", "MB ",};

Double ind = double. parsedouble (in );
Long ind_left = (long) IND;
Long [] inte = new long [13]; // stores the entire number of digits.
Int [] floa = new int [4]; // stores decimal places

/* Place the integer part in inte [] */
For (INT I = 12; I> = 0; I --){
Inte [I] = (long) (ind_left/(long) (math. Pow (10, I )));
Ind_left % = (long) (math. Pow (10, I ));
}
/* Put the fractional part of the image into floa [] */
Int Index = in. indexof ('.');
If (index! =-1 ){
String STR = in. substring (index + 1 );
For (INT I = 0; I <Str. Length (); I ++ ){
Floa [I] = integer. parseint (Str. substring (I, I + 1 ));
}
}
/** If the integer is not 0, the example of processing the entire number is 111.1234 **/
If (INT) ind! = 0 ){
Int I = 12;
While (inte [I] = 0) {file: // search for the first place
I --;
}
/* Output to out */
For (Int J = I/4; j> = 0; j --){
If (j = 3) {// if the number is 13 digits, process 1 MB separately
Out. append (CHN [(INT) (inte [12]). append (mid [3]);
J --;
}
For (int K = 3; k> = 0; k --){
Int num = 4 * j + K;
If (inte [num]! = 0) {// no more than 0 bits last time add
Out. append (CHN [(INT) (inte [num]). append (end [k]);
}
Else {
If (k = 0 | K = 0 & K = 1 | K = 0 & K = 1 & K = 2 |
K = 0 & K = 1 & K = 2 & K = 3) {} // if the last few digits of a trillion are all 0, no output is made.
Else {
If (inte [num-1] = 0) {} // judge whether to add or not 0 based on the following assumption: 1003
Else {
Out. append (CHN [0]);
}
}
}
}
If (! (Inte [J * 4 + 0] = 0 & inte [J * 4 + 1] = 0 &&
Inte [J * 4 + 2] = 0 & inte [J * 4 + 3] = 0) {// if there is a segment that is all 0, it will not be outputted. Example: 1000, 0000, 0003, 100 billion, and
Out. append (mid [J]);
}
}
If (Out. charat (0) = '0') {// because the last digit does not have the next one, if it is 0, delete it.
Out. deletecharat (0 );
} // Example 30
}
/** If the integer is 0, add 0 directly to the integer. Example: 0.1234 **/
Else {
Out. append ('0 ');
}

/* Process the fractional part */
/* If there are decimal places */
If (index! =-1 ){
Out. append ('point'); // process decimal places
For (INT m = 0; m <= 3; m ++ ){
Out. append (CHN [floa [m]);
}
Int zero = out. Length ()-1;
While (Out. charat (zero) = '0') {// Delete the last few digits if they are 0
Out. deletecharat (zero );
Zero --;
}
}
Return out. tostring ();
}
}

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.