Java amount conversion

Source: Internet
Author: User
Note: This program is adapted based on the Program Optimization of a netizen on the Internet. Because the user's Website Cannot be remembered, the original source is not specified. Sorry!
Package COM. work. util;/*** @ author wangmj * written at: 2005-8-2 <br/> * optimized: 2008-07-03 * Class Name: RMB. java <br/> * relatively complete solution. Test passed. <Br/> * the most accurate method is to use a maximum of 13 digits before the decimal point. There is no limit to the number after the decimal point. Of course, it is useless to write too much. Haha. <Br/> */import Java. text. decimalformat; import Java. text. numberformat; // overall idea: // The number is divided into 4 levels. // After classification, each level is processed separately. After processing, strings are connected. // For example: 123456 = 12 | 3456 // Level 2: 12 = + // Level 1: 3456 = Jin qisi Bai Wu Lu + "" public final class RMB {private double amount = 0.0d; Private Static final string num = ""; private Static final string unit = "Getting started"; Private Static final string gradeunit = ""; Private Static final string do Tunit = ""; Private Static final int grade = 4; Private Static final string Sign = "¥"; Private Static final numberformat NF = new decimalformat ("#0. ### ");/*** construction method with parameters ** @ Param amount */private RMB (double amount) {This. amount = amount;} public static string tobigamt (double amount) {NF. setminimumfractiondigits (3); // Add zero after decimal point string AMT = NF. format (amount); // format the number of the double type and convert it to a string. In fact, four Turn five into system. out. println (AMT); double D = new double (amount); string dotpart = ""; // use the decimal point string intpart = ""; // use the integer int dotpos; if (dotpos = AMT. indexof ('. '))! =-1) {intpart = AMT. substring (0, dotpos); dotpart = AMT. substring (dotpos + 1); If (dotpart. length ()> 4) {// more than 4 digits directly intercept dotpart = dotpart. substring () ;}} else {intpart = AMT;} If (intpart. length ()> 16) throw new Java. lang. internalerror ("the amount is too large to be converted. "); String intbig = inttobig (intpart); string dotbig = dottobig (dotpart); // the following code is slightly modified, which is much better now. If (dotbig. Length () = 0) & (intbig. Length ()! = 0) {return intbig + "";} else if (dotbig. length () = 0) & (intbig. length () = 0) {return intbig + "Zero yuan";} else if (dotbig. length ()! = 0) & (intbig. Length ()! = 0) {return intbig + "" + dotbig;} else {return dotbig ;}} /*** is used to process a few cents * @ Param dotpart * @ return */Private Static string dottobig (string dotpart) {// obtain the converted upper case (decimal part) string strret = ""; for (INT I = 0; I <dotpart. length () & I <3; I ++) {int num; If (num = integer. parseint (dotpart. substring (I, I + 1 )))! = 0) strret + = num. substring (Num, num + 1) + dotunit. substring (I, I + 1);} return strret;}/*** used to process the amount of RMB, this requires careful consideration * @ Param intpart * @ return */Private Static string inttobig (string intpart) {// obtain the converted uppercase (integer) int grade; // level length string result = ""; string strtmp = ""; // obtain the level length grade = intpart. length ()/grade; // adjust the level length if (intpart. length () % grade! = 0) grade + = 1; // process the words of each level, first processing the highest level, and then processing the low-level for (INT I = Grade; I> = 1; I --) {strtmp = getnowgradeval (intpart, I); // obtain the current level number result + = getsubunit (strtmp); // convert the value to uppercase. // system. out. println (strtmp + "|" + getsubunit (strtmp); Result = dropzero (result); // remove the consecutive zero by Division of zero // system. out. println ("result =" + result); // level unit if (I> 1) // the last position does not contain units // the unit cannot be continuous // note: special processing is required for four consecutive zeros (wmj modifies this bug) if (getsubunit (strtmp ). equals ("zero") {re Sult = Result + "zero";} else {result + = gradeunit. substring (I-1, I) ;}} return result;} Private Static string getnowgradeval (string strval, int grade) {// obtain the string rst of the current level; if (strval. length () <= Grade * grade) rst = strval. substring (0, strval. length ()-(grade-1) * grade); else rst = strval. substring (strval. length ()-grade * grade, strval. length ()-(grade-1) * grade); Return RST;} priv Ate static string getsubunit (string strval) {// string rst = ""; for (INT I = 0; I <strval. length (); I ++) {string S = strval. substring (I, I + 1); int num = integer. parseint (s); If (num = 0) {// "zero" for special processing if (I! = Strval. length () // the last number after conversion cannot be zero rst + = "zero ";} else {// If intkey = 1 and I = 2 then // "pick up" for special processing // "pick up" reasonable // else rst + = num. substring (Num, num + 1); // end if // append unit if (I! = Strval. length ()-1) // The unit rst + = unit is not added for a single bit. substring (I + 4-strval. length (), I + 4-strval. length () + 1) ;}} return RST;}/***** @ Param strval * @ return */Private Static string dropzero (string strval) {// remove the "zero" string strrst; string strbefore; // string strnow; // The Current Position Character strbefore = strval. substring (0, 1); strrst = strbefore; For (INT I = 1; I <strval. length (); I ++) {strnow = strval. substring (I, I + 1); If (strnow. equals ("zero") & strbefore. equals ("zero"); // zero else strrst + = strnow; strbefore = strnow ;}// the last bit is zero if (strrst. substring (strrst. length ()-1, strrst. length ()). equals ("zero") strrst = strrst. substring (0, strrst. length ()-1); Return strrst;} public static void main (string [] ARGs) {system. out. println (RMB. tobigamt (0.090d); system. out. println (RMB. tobigamt (10.090d); system. out. println (RMB. tobigamt (9.090d); system. out. println (RMB. tobigamt (9.290d); system. out. println (RMB. tobigamt (5.90d); // system. out. println (RMB. tobigamt (10052345.00d); // system. out. println (RMB. tobigamt (0.00d); // system. out. println (RMB. tobigamt (0.60d); // system. out. println (RMB. tobigamt (00.60d); // system. out. println (RMB. tobigamt (150.2101d); // system. out. println (RMB. tobigamt (15400089666.234d); // system. out. println (RMB. tobigamt (15400089666.2mongod); // system. out. println (RMB. tobigamt (1111222233334444.2g0d); system. out. println (RMB. tobigamt (111222233334444.2g0d); system. out. println (RMB. tobigamt (11222233334444.2g0d); system. out. println (RMB. tobigamt (1222233334444.2g0d); system. out. println (RMB. tobigamt (222233334444.2g0d); // system. out. println (RMB. tobigamt (33334444.2mongod); // Java. math. bigdecimal BG = new Java. math. bigdecimal (1111222233334444.2127d); // system. out. println (BG. tostring (); // 1111222233334444.25 bigdecimal is not very accurate system. out. println (RMB. tobigamt. out. println (RMB. tobigamt (10004.2mongod); system. out. println (RMB. tobigamt (22200000004.20000d); system. out. println (RMB. tobigamt (10400.0047d); system. out. println (RMB. tobigamt (2017100000000.23477777777777d); system. out. println (RMB. tobigamt (1001001001000090566d); // you can earn RMB/yuan/Yuan }}

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.