Algorithm for converting the capital amount of RMB

Source: Internet
Author: User

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:

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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:

    1. 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 );
    2. Split the input into integer and decimal parts;
    3. In reverse order, the decimal part is processed first, and then the integer part is processed;
    4. After the conversion, the excess "0" words are processed according to the RMB capital amount specification.

For details, seeCode:

  1. PackageAvstudio. utils;
  2. ImportJava. Text. decimalformat;
  3. /**
  4. * Program name: convertrmb <br/>
  5. * 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/>
  6. * Date: 2011-10-19 <br/>
  7. * Email: chialvin.chan@gmail.com <br/>
  8. * Copyright (C), 2011-2012, chialvin. Chan <br/>
  9. * @ Author chialvin. Chan
  10. * @ Version 1.0
  11. */
  12. PublicClassConvertrmb {
  13. /**
  14. * @ Param d The amount to be converted
  15. * @ Return returns an uppercase string (string)
  16. */
  17. PublicStaticString convert (DoubleD ){
  18. String [] numtables =NewString [] {"Zero","1","II","San","Si","Wu","Lu","Province","Province","Province"};
  19. String [] unittables =NewString [] {"Points",""};
  20. String [] leveltables =NewString [] {"Ten Thousand","Million"};
  21. String [] multables =NewString [] {"","Pick up","Province","Province"};
  22. Stringbuffer result =NewStringbuffer ();
  23. IntIndex =-1;
  24. // Format the number as XXXX. xx
  25. Decimalformat df =NewDecimalformat ();
  26. DF. setgroupingsize (4);
  27. DF. setminimumfractiondigits (2);
  28. String strformat = DF. Format (d );
  29. // Split the integer part and the decimal part
  30. Stringbuffer intpart =NewStringbuffer (strformat. substring (0, Strformat. Length ()-3));
  31. Stringbuffer decimalpart =NewStringbuffer (strformat. substring (intpart. Length () +1, Strformat. Length ()));
  32. // Process the fractional part
  33. Decimalpart. Reverse ();
  34. For(IntI =0; I <decimalpart. Length (); I ++ ){
  35. Result. append (unittables [I %2]);
  36. Result. append (numtables [character. getnumericvalue (decimalpart. charat (I)]);
  37. }
  38. // Process the integer part
  39. Result. append ("Yuan");
  40. Intpart. Reverse ();
  41. IntLevel =0;
  42. For(IntI =0; I <intpart. Length (); I ++ ){
  43. If(Intpart. charat (I )! =','){
  44. Result. append (multables [I %5]);
  45. Result. append (numtables [character. getnumericvalue (intpart. charat (I)]);
  46. }Else{
  47. Result. append (leveltables [level]);
  48. Level = ++ level %2;
  49. }
  50. }
  51. Result. Reverse ();
  52. // Process unnecessary zero
  53. While(Index = result. indexof ("Zero score"))! =-1) {Result. deletecharat (index +1);};
  54. While(Index = result. indexof ("Zero angle"))! =-1) {Result. deletecharat (index +1);};
  55. While(Index = result. indexof ("Zero pick"))! =-1) {Result. deletecharat (index +1);};
  56. While(Index = result. indexof ("Zero latency"))! =-1) {Result. deletecharat (index +1);};
  57. While(Index = result. indexof ("Zero latency"))! =-1) {Result. deletecharat (index +1);};
  58. // No decimal part
  59. While(Index = result. indexof ("Zero RMB"))! =-1){
  60. Result. Delete (index +1, Index +3);
  61. Result. append ("Whole");
  62. };
  63. While(Index = result. indexof ("Zero"))! =-1) {Result. deletecharat (INDEX );};
  64. While(Index = result. indexof ("Zero RMB"))! =-1) {Result. deletecharat (INDEX );};
  65. While(Index = result. indexof ("0 thousand"))! =-1) {Result. deletecharat (INDEX );};
  66. While(Index = result. indexof ("0 billion"))! =-1) {Result. deletecharat (INDEX );};
  67. While(Index = result. indexof ("Hundreds of millions"))! =-1) {Result. deletecharat (index +1);};
  68. // No score
  69. While(Index = result. indexof ("Corner zero"))! =-1) {Result. deletecharat (index +1);};
  70. // Only minutes are allowed.
  71. While(Index = result. indexof ("RMB 0"))! =-1& Index =0) {Result. Delete (index, index +2);};
  72. // Only supports decimal places
  73. While(Index = result. indexof ("Yuan"))! =-1& Index =0) {Result. deletecharat (INDEX );};
  74. // Zero RMB
  75. While(Index = result. indexof ("Whole"))! =-1& Index =0) {Result. Replace (index, index +2,"Zero RMB");};
  76. ReturnResult. tostring ();
  77. }
  78. PublicStaticVoidMain (string [] ARGs ){
  79. // Todo auto-generated method stub
  80. System. Out. println (convertrmb. Convert (1680.32));
  81. System. Out. println (convertrmb. Convert (1409.50));
  82. System. Out. println (convertrmb. Convert (6007.14));
  83. System. Out. println (convertrmb. Convert (107000.53));
  84. System. Out. println (convertrmb. Convert (16409.02));
  85. System. Out. println (convertrmb. Convert (325.04));
  86. System. Out. println (convertrmb. Convert (0.01));
  87. System. Out. println (convertrmb. Convert (2.10));
  88. System. Out. println (convertrmb. Convert (0.49));
  89. System. Out. println (convertrmb. Convert (1.49));
  90. System. Out. println (convertrmb. Convert (1D ));
  91. System. Out. println (convertrmb. Convert (0d ));
  92. System. Out. println (convertrmb. Convert (1234567891012d ));
  93. System. Out. println (convertrmb. Convert (9000000000000d ));
  94. System. Out. println (convertrmb. Convert (9010000000005d ));
  95. System. Out. println (convertrmb. Convert (9999999999999998d ));
  96. }
  97. }

 

Output:

    1. points of interest collected by Lu Yi
    2. yuan Wu Jiao, a hacker,
    3. Lu xiaoqianyuan yijiaosi
    4. tens of thousands of RMB per cent
    5. tens of thousands of Lu xiaosi yuan 0 points 0 Points 2 points
    6. collect RMB and Points
    7. 1 minute
    8. yuan Yi Jiao
    9. Points
    10. yiyuan Si Jiao Min
    11. RMB 1
    12. zero RMB
    13. tens of thousands of yuan worth of Yuan
    14. trillions of RMB
    15. tens of thousands of workers and hundreds of millions of workers
    16. 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

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.