Package cyachina. util;
/**
* Created by intellij idea.
* User: Administrator
* Date: 2004-8-2
* Time: 11:37:41
* To change this template use file | Settings | file templates.
*/
Public class moneyutil {
Public static string [] chinesedigits = new string [] {"zero", "one", "two", "three", "Si", "Wu", "Lu ", "identifier", "identifier", "identifier "};
/**
* Convert the amount to the number expressed in Chinese characters. after the decimal point, two digits are rounded to the nearest decimal point.
* @ Param amount
* @ Return
*/
Public static string amounttochinese (double amount ){
If (amount> 99999999999999.99 | amount <-99999999999999.99)
Throw new illegalargumentexception ("the parameter value exceeds the permitted range (-99999999999999.99 ~ 99999999999999.99 )! ");
Boolean negative = false;
If (amount <0 ){
Negative = true;
Amount = Amount * (-1 );
}
Long temp = math. Round (Amount * 100 );
Int numfen = (INT) (TEMP % 10); // minute
Temp = temp/10;
Int numjiao = (INT) (TEMP % 10); // corner
Temp = temp/10;
// Temp is currently an integer part of the amount
Int [] parts = new int [20]; // The element is to divide the integer part of the original amount into 0 ~ Each part of the number between 9999
Int numparts = 0; // The record splits the integer part of the original amount into several parts (each part is between 0 and ~ Between 9999)
For (INT I = 0; I ++ ){
If (temp = 0)
Break;
Int part = (INT) (temp %10000 );
Parts [I] = part;
Numparts ++;
Temp = temp/10000;
}
Boolean beforewaniszero = true; // indicates whether the level below "" is 0
String chinesestr = "";
For (INT I = 0; I <numparts; I ++ ){
String partchinese = parttranslate (parts [I]);
If (I % 2 = 0 ){
If ("". Equals (partchinese ))
Beforewaniszero = true;
Else
Beforewaniszero = false;
}
If (I! = 0 ){
If (I % 2 = 0)
Chinesestr = "" + chinesestr;
Else {
If ("". Equals (partchinese )&&! Beforewaniszero) // if the part corresponding to "" is 0, and the level below "" is not 0, "" is not added, and "zero" is added"
Chinesestr = "0" + chinesestr;
Else {
If (parts [I-1] <1000 & parts [I-1]> 0) // if the "" part is not 0, if the first part of "1000" is smaller than and greater than 0, the second part should be followed by "zero"
Chinesestr = "0" + chinesestr;
Chinesestr = "Ten Thousand" + chinesestr;
}
}
}
Chinesestr = partchinese + chinesestr;
}
If ("". Equals (chinesestr) // If the integer is 0, it is expressed as "Zero RMB"
Chinesestr = chinesedigits [0];
Else if (negative) // The integer part is not 0, and the original amount is negative.
Chinesestr = "negative" + chinesestr;
Chinesestr = chinesestr + "Yuan ";
If (numfen = 0 & numjiao = 0 ){
Chinesestr = chinesestr + "whole ";
}
Else if (numfen = 0) {// 0, not 0
Chinesestr = chinesestr + chinesedigits [numjiao] + "";
}
Else {// The number of "points" is not 0
If (numjiao = 0)
Chinesestr = chinesestr + "zero" + chinesedigits [numfen] + "points ";
Else
Chinesestr = chinesestr + chinesedigits [numjiao] + "" + chinesedigits [numfen] + "points ";
}
Return chinesestr;
}
/**
* Set a value of 0 ~ An integer between 9999 to a Chinese character string. If it is 0, "" is returned ""
* @ Param amountpart
* @ Return
*/
Private Static string parttranslate (INT amountpart ){
If (amountpart <0 | amountpart> 10000 ){
Throw new illegalargumentexception ("the parameter must be an integer greater than or equal to 0 and less than 10000! ");
}
String [] Units = new string [] {"", "pick up", "success", "success "};
Int temp = amountpart;
String amountstr = new INTEGER (amountpart). tostring ();
Int amountstrlength = amountstr. Length ();
Boolean lastiszero = true; // records whether the previous digit is 0 in a cycle from low to high.
String chinesestr = "";
For (INT I = 0; I <amountstrlength; I ++ ){
If (temp = 0) // No data exists in the high position
Break;
Int digit = TEMP % 10;
If (digit = 0) {// The number obtained is 0.
If (! Lastiszero) // if the previous number is not 0, add the word "0" before the current Chinese character string;
Chinesestr = "0" + chinesestr;
Lastiszero = true;
}
Else {// The number obtained is not 0
Chinesestr = chinesedigits [digit] + units [I] + chinesestr;
Lastiszero = false;
}
Temp = temp/10;
}
Return chinesestr;
}
Public static void main (string [] ARGs ){
If (ARGs. Length = 0 ){
System. Out. println ("conversion Demo :");
System. Out. println ("-------------------------");
System. Out. println ("25000000000005.999:" + amounttochinese (25000000000005.999 ));
System. Out. println ("45689263.626:" + amounttochinese (45689263.626 ));
System. Out. println ("0.69457:" + amounttochinese (0.69457 ));
System. Out. println ("253.0:" + amounttochinese (253.0 ));
System. Out. println ("0:" + amounttochinese (0 ));
System. Out. println ("-------------------------");
System. Out. println ("usage of this program :");
System. Out. println ("Demo: Java moneyutil ");
System. Out. println ("conversion specified number: Java moneyutil [number to be converted]");
// System. Out. println (long. max_value );
// System. Out. println (long. min_value );
}
Else {
System. Out. println ("Conversion Result :");
System. Out. println (ARGs [0] + ":" + amounttochinese (double. parsedouble (ARGs [0]);
}
}
}