A small example of how to convert a floating point to a Renminbi in java.

Source: Internet
Author: User

A small example of how to convert a floating point to a Renminbi in java.

Copy codeThe Code is as follows:
Import java. util. ArrayList;
Import java. util. List;

Public class RMBConverter2 implements IRMBConverter {

Private static final String [] RMB _NUMBER = {"zero", "one", "two", "three", "Si", "Wu", "Lu ", "identifier", "identifier", "identifier "};

Private static final String [] RMB _ATTR = {"Yuan", "Ten", "Hundred ", "Category", "million", "10", "category", "category", "", "10", "category", "category "};

Private static final String [] RMB _FEN = {"points", "Angles "};


@ Override
Public String change2RMB (double counts ){
StringBuffer rsBuf = new StringBuffer ();

If (counts = 0L ){
Return "zero ";
}

If (counts <0)
RsBuf. append ("negative ");

String zheng = strConverse (String. valueOf (long) counts ));
String xiao = strConverse (String. valueOf (Math. round (counts-(long) counts) * 100 )));

Char [] zhengArray = zheng. toCharArray ();
Char [] xiaoArray = xiao. toCharArray ();

Int zLength = zhengArray. length;
Int xLength = xiaoArray. length;

List <String> zhengList = new ArrayList <String> ();

For (int I = 0; I <zLength; I ++ ){
Int value = Integer. parseInt ("" + zhengArray [I]);
If (value = 0 & (I! = 4) & (I! = 8) & I! = 0 ){
ZhengList. add (RMB _NUMBER [value]);
} Else {
ZhengList. add (RMB _NUMBER [value] +
RMB _ATTR [I]);
}
}

For (int I = zhengList. size ()-1; I> = 0; I --){
RsBuf. append (zhengList. get (I ));
}


For (int I = 0; I <xLength; I ++ ){
Int value = Integer. parseInt ("" + xiaoArray [I]);
If (value> 0 ){
RsBuf. append (RMB _NUMBER [value])
. Append (RMB _FEN [xLength-i-1]);
}
}

String rs = rsBuf. toString ();
Rs = rs. replaceAll ("Zero +", "zero ");
Rs = rs. replaceAll ("0 RMB", "RMB ");
Rs = rs. replaceAll ("0 thousand ");
Rs = rs. replaceAll ("0 billion ");
Rs = rs. replaceAll ("hundreds of millions", "hundreds of millions ");
Return rs;
}


Private String strConverse (String str ){
StringBuffer rs = new StringBuffer ();
Char [] strArray = str. toCharArray ();
For (int I = str. length ()-1; I> = 0; I --){
Rs. append (strArray [I]);
}
Return rs. toString ();
}


Public static void main (String args []) {

RMBConverter2 c2 = new RMBConverter2 ();
System. out. println (c2.change2RMB (2001004.10 ));
System. out. println (c2.change2RMB (2000000.10 ));
System. out. println (c2.change2RMB (0L ));
System. out. println (c2.change2RMB (commandid 0000000l ));
}

}

Related Article

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.