A class for converting numbers into numbers, commonly used in RMB!

Source: Internet
Author: User
  1. Package lihan;
  2. Public class moneyutil {
  3. /** Uppercase numbers */
  4. Private Static final string [] numbers = {"zero", "one", "two", "three", "Si", "Wu", "Lu ",
  5. "Identifier", "identifier", "identifier "};
  6. /** Unit of the integer part */
  7. Private Static final string [] iunit = {"Yuan", "pick up", "average", "average", "Ten Thousand", "pick up", "average ",
  8. "Proportion", "million", "Pick", "weight", "weight", "", "Pick", "weight", "weight "};
  9. /** Unit of the decimal part */
  10. Private Static final string [] dunit = {"", "points", ""};
  11. /**
  12. * Obtain the capital amount.
  13. */
  14. Public static string tochinese (string Str ){
  15. STR = Str. replaceall (",", ""); // remove ","
  16. String integerstr; // integer part number
  17. String decimalstr; // decimal digit
  18. // Initialization: Separate the integer part and the decimal part.
  19. If (Str. indexof (".")> 0 ){
  20. Integerstr = Str. substring (0, str. indexof ("."));
  21. Decimalstr = Str. substring (Str. indexof (".") + 1 );
  22. } Else if (Str. indexof (".") = 0 ){
  23. Integerstr = "";
  24. Decimalstr = Str. substring (1 );
  25. } Else {
  26. Integerstr = STR;
  27. Decimalstr = "";
  28. }
  29. // Remove the first 0 from integerstr, and remove the last 0 from decimalstr (more than partial removal)
  30. If (! Integerstr. Equals ("")){
  31. Integerstr = long. tostring (long. parselong (integerstr ));
  32. If (integerstr. Equals ("0 ")){
  33. Integerstr = "";
  34. }
  35. }
  36. // Overflow exceeds the processing capability and returns directly
  37. If (integerstr. Length ()> iunit. Length ){
  38. System. Out. println (STR + ": exceeds processing capability ");
  39. Return STR;
  40. }
  41. Int [] integers = toarray (integerstr); // integer part number
  42. Boolean ismust5 = ismust5 (integerstr); // sets the unit of 10 thousand
  43. Int [] decimals = toarray (decimalstr); // decimal digit
  44. Return getchineseinteger (integers, ismust5) + getchinesedecimal (decimals );
  45. }
  46. /**
  47. * Convert integer and decimal parts to an array, from high to low
  48. */
  49. Private Static int [] toarray (string number ){
  50. Int [] array = new int [number. Length ()];
  51. For (INT I = 0; I <number. Length (); I ++ ){
  52. Array [I] = integer. parseint (number. substring (I, I + 1 ));
  53. }
  54. Return array;
  55. }
  56. /**
  57. * Obtain the integer of the Chinese amount.
  58. */
  59. Private Static string getchineseinteger (INT [] integers, Boolean ismust5 ){
  60. Stringbuffer chineseinteger = new stringbuffer ("");
  61. Int length = integers. length;
  62. For (INT I = 0; I <length; I ++ ){
  63. // 0 appears in key locations: 1234 (5678) 9012 (3456) (yuan)
  64. // Special case: 10 (RMB collection, and RMB collection)
  65. String key = "";
  66. If (integers [I] = 0 ){
  67. If (length-I) = 13) // () (required)
  68. Key = iunit [4];
  69. Else if (length-I) = 9) // (required)
  70. Key = iunit [8];
  71. Else if (length-I) = 5 & ismust5) // (not required)
  72. Key = iunit [4];
  73. Else if (length-I) = 1) // yuan (required)
  74. Key = iunit [0];
  75. // If the value of 0 is not 0, it is set to zero and does not contain the last digit.
  76. If (length-I)> 1 & integers [I + 1]! = 0)
  77. Key + = numbers [0];
  78. }
  79. Chineseinteger. append (integers [I] = 0? Key
  80. : (Numbers [integers [I] + iunit [length-I-1]);
  81. }
  82. Return chineseinteger. tostring ();
  83. }
  84. /**
  85. * Obtain the fractional part of the Chinese amount.
  86. */
  87. Private Static string getchinesedecimal (INT [] decimals ){
  88. Stringbuffer chinesedecimal = new stringbuffer ("");
  89. For (INT I = 0; I <decimals. length; I ++ ){
  90. // Remove the three decimal places
  91. If (I = 3)
  92. Break;
  93. Chinesedecimal. append (decimals [I] = 0? ""
  94. : (Numbers [decimals [I] + dunit [I]);
  95. }
  96. Return chinesedecimal. tostring ();
  97. }
  98. /**
  99. * Determines whether the Unit of the 5th-digit number is.
  100. */
  101. Private Static Boolean ismust5 (string integerstr ){
  102. Int length = integerstr. Length ();
  103. If (length> 4 ){
  104. String subinteger = "";
  105. If (length> 8 ){
  106. // Obtain the string from the low-digit, 5th-8th bits
  107. Subinteger = integerstr. substring (length-8, length-4 );
  108. } Else {
  109. Subinteger = integerstr. substring (0, length-4 );
  110. }
  111. Return integer. parseint (subinteger)> 0;
  112. } Else {
  113. Return false;
  114. }
  115. }
  116. Public static void main (string [] ARGs ){
  117. String number = "54452 ";
  118. System. Out. println (number + "" + moneyutil. tochinese (number ));
  119. Number = "30200 ";
  120. System. Out. println (number + "" + moneyutil. tochinese (number ));
  121. Number = "30000.05 ";
  122. System. Out. println (number + "" + moneyutil. tochinese (number ));
  123. Number = "30000.00 ";
  124. System. Out. println (number + "" + moneyutil. tochinese (number ));
  125. }
  126. }

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.