Programming question 2 in the written test

Source: Internet
Author: User
Convert the numeric amount into a Chinese amount. For example: 23424.12 ----> erwanqianyi si Yuan points. Depressed... turn over (lessons ).

Import java. Math. bigdecimal;

Public class chinesecurrency {
// Array of Chinese AMOUNT UNITS
String [] arrchineseunit = {"Minute", "angle", "circle", "Pick", "Hour", "Ten Thousand", "Pick ", "Province ",
"Proportion", "million", "Pick", "weight", "weight "};
// Chinese numeric character array
String [] arrchinesenum = {"zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "identifier", "identifier "};

/**
* Description converts the numeric amount to the Chinese amount
*
* @ Param bigdecimal
* Number before bigdmoneynumber Conversion
* @ Return string call: <br/>
* Mytochinesecurrency ("101.89") = ""
* Mytochinesecurrency ("100.89") = ""
* Mytochinesecurrency ("100") = ""
*/
Public String donumbercurrencytochinesecurrency (bigdecimal moneynumber ){
String strchinesecurrency = "";
// Zero-digit mark
Boolean iszero = true;
// Chinese amount unit subscript
Int chineseunitindex = 0;
Try {
If (moneynumber. intvalue () = 0 ){
Return "Zero circle ";
}
// Process the decimal part, rounded down
Double doubmoneynumber = math
. Round (moneynumber. doublevalue () * 100 );
// Whether it is negative
Boolean bnegative = doubmoneynumber <0;
// Obtain the absolute value
Doubmoneynumber = math. Abs (doubmoneynumber );

// Process the conversion operation cyclically
While (doubmoneynumber> 0 ){
// Processing (no decimal places)
If (chineseunitindex = 2 & strchinesecurrency. Length () = 0)
Strchinesecurrency = strchinesecurrency + "whole ";

// Processing of non-zero digits
If (doubmoneynumber % 10> 0 ){
Strchinesecurrency = arrchinesenum [(INT) doubmoneynumber % 10]
+ Arrchineseunit [chineseunitindex]
+ Strchinesecurrency;
Iszero = false;
}
// Zero-Digit Processing
Else {
// Metadata processing (single digit)
If (chineseunitindex = 2 ){
// The segment contains numbers
If (doubmoneynumber> 0 ){
Strchinesecurrency = arrchineseunit [chineseunitindex]
+ Strchinesecurrency;
Iszero = true;
}
}
// Processing of tens of thousands or hundreds of millions of digits
Else if (chineseunitindex = 6 | chineseunitindex = 10 ){
// The segment contains numbers
If (doubmoneynumber % 1000> 0)
Strchinesecurrency = arrchineseunit [chineseunitindex]
+ Strchinesecurrency;
}
// Non-zero processing of the previous Digit
If (! Iszero ){
Strchinesecurrency = arrchinesenum [0]
+ Strchinesecurrency;
}
Iszero = true;
}
Doubmoneynumber = math. Floor (doubmoneynumber/10 );
Chineseunitindex ++;
}
// Negative number processing
If (bnegative)
Strchinesecurrency = "negative" + strchinesecurrency;
} Catch (exception e ){

}
Return strchinesecurrency;
}
}

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.