An exercise in crazy Java handout: convert a floating point number into a reading string of RMB, for example, convert 1006.33 into a zero-point pair.
Note the following points for Conversion Based on the RMB capital amount specification:
- When the Arabic numerals are 0, only one "zero" word can be written in the middle of the Chinese capital amount. For example, ¥1,409.50 should be written as RMB yuan.
- There are several consecutive "0" in the middle of Arabic numerals, and only one "0" word can be written in the middle of the capital amount in Chinese. For example, ¥6,007.14 should be written as RMB Lu Yi and Yuan Yi Jiao Si.
- The value of the Arabic dollar number is "0", or the number contains several consecutive "0", and the value of the ten thousand or the dollar is also "0 ", however, when the margin and corner are not "0", you can write only one zero word or no "zero" word in the Chinese capital, for example, ¥1,680.32, it should be written as RMB, RMB, or $107,000.53, it should be written as RMB yuan, or RMB yuan.
- When the Arabic value is 0 and the split is not 0, the Chinese capital value "Yuan" should be followed by "0. For example, ¥16,409.02 should be written as RMB 325.04 million yuan, and RMB should be written as RMB million yuan.
- If the capital amount ends with "Yuan", the word "whole" or "positive" should be written after "Yuan, after the "angle", you can not write the "whole" or "positive" word, but do not write the "whole" or "positive" word after the "points.
My thinking is:
- Format the input to "XXXX, xxxx, xx", divide the integer part by a thousand bits, and retain two digits (to the minute) in the decimal part );
- Split the input into integer and decimal parts;
- In reverse order, the decimal part is processed first, and then the integer part is processed;
- After the conversion, the excess "0" words are processed according to the RMB capital amount specification.
For details, seeCode:
- PackageAvstudio. utils;
-
-
- ImportJava. Text. decimalformat;
-
-
- /**
-
- * Program name: convertrmb <br/>
-
- * Description: Convert the floating point amount to the RMB reading method, accurate to the minute, for example, input: 1006.33, output: the value ranges from 0 to 9999999999999998. <br/>
-
- * Date: 2011-10-19 <br/>
-
- * Email: chialvin.chan@gmail.com <br/>
-
- * Copyright (C), 2011-2012, chialvin. Chan <br/>
- * @ Author chialvin. Chan
-
- * @ Version 1.0
-
- */
-
-
- PublicClassConvertrmb {
-
-
- /**
-
- * @ Param d The amount to be converted
-
- * @ Return returns an uppercase string (string)
-
- */
-
- PublicStaticString convert (DoubleD ){
- String [] numtables =NewString [] {"Zero","1","II","San","Si","Wu","Lu","Province","Province","Province"};
-
- String [] unittables =NewString [] {"Points",""};
-
- String [] leveltables =NewString [] {"Ten Thousand","Million"};
- String [] multables =NewString [] {"","Pick up","Province","Province"};
-
- Stringbuffer result =NewStringbuffer ();
-
- IntIndex =-1;
-
-
- // Format the number as XXXX. xx
-
- Decimalformat df =NewDecimalformat ();
-
- DF. setgroupingsize (4);
-
- DF. setminimumfractiondigits (2);
- String strformat = DF. Format (d );
-
-
- // Split the integer part and the decimal part
-
- Stringbuffer intpart =NewStringbuffer (strformat. substring (0, Strformat. Length ()-3));
-
- Stringbuffer decimalpart =NewStringbuffer (strformat. substring (intpart. Length () +1, Strformat. Length ()));
-
-
- // Process the fractional part
-
- Decimalpart. Reverse ();
- For(IntI =0; I <decimalpart. Length (); I ++ ){
-
- Result. append (unittables [I %2]);
-
- Result. append (numtables [character. getnumericvalue (decimalpart. charat (I)]);
-
- }
-
-
- // Process the integer part
-
- Result. append ("Yuan");
-
- Intpart. Reverse ();
-
- IntLevel =0;
- For(IntI =0; I <intpart. Length (); I ++ ){
-
- If(Intpart. charat (I )! =','){
-
- Result. append (multables [I %5]);
-
- Result. append (numtables [character. getnumericvalue (intpart. charat (I)]);
-
- }Else{
-
- Result. append (leveltables [level]);
-
- Level = ++ level %2;
-
- }
-
- }
-
- Result. Reverse ();
-
-
- // Process unnecessary zero
-
- While(Index = result. indexof ("Zero score"))! =-1) {Result. deletecharat (index +1);};
-
- While(Index = result. indexof ("Zero angle"))! =-1) {Result. deletecharat (index +1);};
-
- While(Index = result. indexof ("Zero pick"))! =-1) {Result. deletecharat (index +1);};
- While(Index = result. indexof ("Zero latency"))! =-1) {Result. deletecharat (index +1);};
-
- While(Index = result. indexof ("Zero latency"))! =-1) {Result. deletecharat (index +1);};
-
- // No decimal part
-
- While(Index = result. indexof ("Zero RMB"))! =-1){
-
- Result. Delete (index +1, Index +3);
- Result. append ("Whole");
-
- };
-
- While(Index = result. indexof ("Zero"))! =-1) {Result. deletecharat (INDEX );};
-
- While(Index = result. indexof ("Zero RMB"))! =-1) {Result. deletecharat (INDEX );};
-
- While(Index = result. indexof ("0 thousand"))! =-1) {Result. deletecharat (INDEX );};
- While(Index = result. indexof ("0 billion"))! =-1) {Result. deletecharat (INDEX );};
-
- While(Index = result. indexof ("Hundreds of millions"))! =-1) {Result. deletecharat (index +1);};
-
- // No score
-
- While(Index = result. indexof ("Corner zero"))! =-1) {Result. deletecharat (index +1);};
-
- // Only minutes are allowed.
- While(Index = result. indexof ("RMB 0"))! =-1& Index =0) {Result. Delete (index, index +2);};
-
- // Only supports decimal places
-
- While(Index = result. indexof ("Yuan"))! =-1& Index =0) {Result. deletecharat (INDEX );};
-
- // Zero RMB
- While(Index = result. indexof ("Whole"))! =-1& Index =0) {Result. Replace (index, index +2,"Zero RMB");};
-
-
- ReturnResult. tostring ();
-
- }
-
-
- PublicStaticVoidMain (string [] ARGs ){
-
- // Todo auto-generated method stub
-
- System. Out. println (convertrmb. Convert (1680.32));
- System. Out. println (convertrmb. Convert (1409.50));
-
- System. Out. println (convertrmb. Convert (6007.14));
-
- System. Out. println (convertrmb. Convert (107000.53));
-
- System. Out. println (convertrmb. Convert (16409.02));
-
- System. Out. println (convertrmb. Convert (325.04));
-
- System. Out. println (convertrmb. Convert (0.01));
-
- System. Out. println (convertrmb. Convert (2.10));
- System. Out. println (convertrmb. Convert (0.49));
-
- System. Out. println (convertrmb. Convert (1.49));
-
- System. Out. println (convertrmb. Convert (1D ));
-
- System. Out. println (convertrmb. Convert (0d ));
-
- System. Out. println (convertrmb. Convert (1234567891012d ));
-
- System. Out. println (convertrmb. Convert (9000000000000d ));
-
- System. Out. println (convertrmb. Convert (9010000000005d ));
-
- System. Out. println (convertrmb. Convert (9999999999999998d ));
-
- }
-
-
- }
Output:
- points of interest collected by Lu Yi
- yuan Wu Jiao, a hacker,
- Lu xiaoqianyuan yijiaosi
- tens of thousands of RMB per cent
- tens of thousands of Lu xiaosi yuan 0 points 0 Points 2 points
- collect RMB and Points
- 1 minute
- yuan Yi Jiao
- Points
- yiyuan Si Jiao Min
- RMB 1
- zero RMB
- tens of thousands of yuan worth of Yuan
- trillions of RMB
- tens of thousands of workers and hundreds of millions of workers
- hundreds of millions of users who have collected hundreds of millions of data records and hundreds of millions of users who have collected tens of thousands of data records