Let's take a look at the example of money conversion in crazy Java handouts.

Source: Internet
Author: User

The Code is as follows:

 

Import java. util. Arrays;

Import java. util. collections;

/**

* Description:

* <Br/> website: <a href = "http://www.crazyit.org"> crazy Java Alliance </a>

* <Br/> Copyright (C), 2001-2012, Yeeku. H. Lee

* <Br/> This program is protected by copyright laws.

* <Br/> Program Name:

* <Br/> Date:

* @ Author Yeeku. H. Lee kongyeeku@163.com

* @ Version 1.0

*/

Public class Num2Rmb

{

Private String [] hanArr = {"zero", "one", "two", "three", "Si ",

"Wu", "Lu", "Lu", "Wu", "Lu "};

Private String [] unitArr = {"Ten", "Hundred", "Thousand "};

Private String [] weiArr = "};

 

/**

* Splits a floating point number into integer and decimal strings.

* @ Param num refers to the floating point number to be decomposed.

* @ Return refers to the integer part and decimal part. The first array element is the integer part, and the second array element is the fractional part.

*/

Private String [] divide (double num)

{

// Forcibly convert a floating point to long, that is, obtain its integer

Long zheng = (long) num;

// The floating point minus the integer part to get the fractional part. Multiply the fractional part by 100 and then get the integer to 2 decimal places.

Long xiao = Math. round (num-zheng) * 100); // Math. round (double d) returns the long, Math. round (float f) returns the int closest to f

// The following two methods are used to convert integers into strings.

Return new String [] {zheng + "", String. valueOf (xiao )};

}

 

/**

* Convert a four-digit numeric string into a Chinese character string

* @ Param numStr the four-digit numeric string to be converted

* @ Return the four-digit numeric string to be converted into a Chinese character string.

*/

Private String toHanStr (String numStr)

{

String result = "";

Int numLen = numStr. length ();

// Traverse each digit of a numeric string in sequence

For (int I = 0; I <numLen; I ++)

{

// Convert char-type numbers to int-type numbers because their ASCII values are exactly 48 different

// Reduce the char-type number by 48 to get the int-type number. For example, '4' is converted to 4.

Int num = numStr. charAt (I)-48;

// If it is not the last digit and the number is not zero, you need to add the unit (, 10)

If (I! = NumLen-1 & num! = 0)

{

Result + = hanArr [num] + unitArr [numLen-2-I];

}

// Otherwise, do not add units

Else

{

Result + = hanArr [num];

}

}

Return result;

}

// Fing Modification

// Modify an extra four-character string to multiple four-character strings.

// A string up to 12 characters can be entered.

Private String [] fenJie (String numStr)

{

Int numLen = numStr. length ();

// The judgment should be broken down into several four-character strings

Int temp = (numLen + 3)/4;

String [] s = new String [temp];

For (int I = 0; I <temp; I ++)

S [I] = "";

For (int I = 0; I <numLen; I ++)

{

S [(numLen-1-i)/4] + = numStr. charAt (I );

}

Return s;

}

// The amount of the decimal part

Private String toHan (String numStr)

{

String result = "RMB ";

// Converts a char-type number to an int

// Reduce the char-type number by 48 to get the int-type number. For example, '4' is converted to 4.

Int num = numStr. charAt (0)-'0 ';

If (num! = 0)

Result + = hanArr [num] + 'angular ';

Num = numStr. charAt (1)-'0 ';

If (num! = 0)

Result + = hanArr [num] + 'Min ';

Return result;

}

Public static void main (String [] args)

{

Num2Rmb nr = new Num2Rmb ();

// The test Splits a floating point number into an integer part and a decimal part.

System. out. println (Arrays. toString (nr. divide (236711125.123 )));

// Test to convert a four-digit numeric string into a Chinese character string

// System. out. println (nr. toHanStr ("6109 "));

// Fing Modification

Running in = new Processing (System. in );

Double m = in. nextDouble ();

String [] toM = nr. divide (m );

String [] ts = nr. fenJie (toM [0]);

String re = "";

System. out. println (ts. length );

For (int I = ts. length-1; I> = 0; I --)

{

Re + = nr. toHanStr (ts [I]) + nr. weiArr [I];

}

System. out. println (re + nr. toHan (toM [1]);

In. close ();

}

}

 

The problem with this program is that normal output cannot be achieved when multiple 0 consecutive values exist. Please give me some comments.

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.