Floating point number converted to uppercase currency amount

Source: Internet
Author: User

Tag:java    Currency amount    

/** * Floating-point number converted to uppercase Currency Amount * * @author Bobby * */public class Convertfloatnumtormbformat {private static string[] Uppercasearray  = {"0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "JIU"};p rivate static string[] Moneyunitarray = {"Yuan", "Pick", "Bai", "Qian", "Million", "Pick Up", "Bai", "Qian", "billion", "pick", "Bai", "Thousand", "Million"};p rivate static string[] Decimalarray = {"Angle", "min"};/** * Divide the number into integers and fractional parts * * @pa Ram num * @return string[] */private string[] getdividedstringnum (String num) {int index = 0;for (int i = 0; i < Num.le Ngth (); i++) {if (Num.charat (i) = = '. ') break;index++;} The amount of Integer part string integerstring = num.substring (0, index);//Amount Fractional part (exact to amount) string decialstring = "0"; if (Index < Num.leng Th ()-1) decialstring = num.substring (index + 1); return new string[] {integerstring, decialstring};}  Private string[] Getdividedstringnum (double num) {//Amount integer part long integerdata = (long) num;//amount fractional part (accurate to amount) long decialdata = Math.Round ((num-integerdata) * 100);//round String decialstring = string.valueof (decialdata);//If the amount angle is 0if (DECialdata < 10) {//For example: 0->00decialstring = "0" + decialdata;} Returns the amount of the integer part and fractional part return new string[] {string.valueof (integerdata), decialstring};}  /** * Convert the integer portion of the amount * * @param integerstr * @return string */private string Getintegerstr (String integerstr) {string result = ""; Boolean Iszero = True;int numlength = Integerstr.length (), if (Numlength >) {System.out.println ("Amount beyond the scope of this program");} else {for (int i = 0; i < numlength; i++) {//Get the I position of the number int num = Integerstr.charat (i)-48;if (num! = 0) {Iszero = False ; result + = uppercasearray[num]+ moneyunitarray[numlength-i-1];} else {//If there are multiple consecutive 0 displays a 0, for example 10001 to one zero, not one zero zero if (i > 0&&!result.substring (Result.length ()-1). Equals ("zero ") {result + = Uppercasearray[num];} If there is a number that is not 0, and to the count unit point if (!iszero && (numlength-i-1)% 4 = = 0) {//flag is re-recorded as 0isZero = true;//if the count unit point is 0, remove 0 if (Result.substring (Result.length ()-1). Equals ("0")) {result = result.substring (0, Result.length ()-1);} Add count unit result + = moneyunitarray[(numLength-i-1)/4 * 4];} If the last character is 0 replaced with the meta if (i = = numlength-1&& result.substring (result.length ()-1). Equals ("0")) {result = Result.subs Tring (0, Result.length ()-1) + "Yuan";}}} return result;} private string Getdecimalstr (String decimalstr) {string result = ""; if (null! = DECIMALSTR &&! "). Equals (DECIMALSTR)) {int allnum = Integer.parseint (DECIMALSTR), if (Allnum > 0) {int numlength = Decimalstr.length (); fo R (int i = 0; i < numlength; i++) {int num = Decimalstr.charat (i)-48;if (num! = 0) {result + = Uppercasearray[num] + D Ecimalarray[i];} else {if (I < 1) {result + = Uppercasearray[num];}}} else {result = "whole";}} return result;} Private String getconvertedstring (double moneydata) {string[] Dividedstringnumarray = Getdividedstringnum (Moneydata); String integerstr = dividedstringnumarray[0]; String result = Getintegerstr (INTEGERSTR); if (null! = result &&! "). Equals (Result) {String Decimalstr = Dividedstringnumarray[1];result + = Getdecimalstr (DECIMALSTR);} Return result;} private string Getconvertedstring (String moneystring) {string[] Dividedstringnumarray = Getdividedstringnum ( moneystring); String integerstr = dividedstringnumarray[0]; String result = Getintegerstr (INTEGERSTR); if (null! = result &&! "). Equals (Result) {String Decimalstr = Dividedstringnumarray[1];result + = Getdecimalstr (DECIMALSTR);} return result;} public static void Main (string[] args) {Convertfloatnumtormbformat nr = new Convertfloatnumtormbformat ();d ouble Moneydata = 10000000001.01; String moneystring = "10000000001"; System.out.println (nr.getconvertedstring (moneydata)); System.out.println (nr.getconvertedstring (moneystring));} <pre>}

Output Result:

One hundred billion zero one yuan 0 cents
Yi Bai billion zero one yuan whole



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Floating point number converted to uppercase currency amount

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.