The conversion of the meta and the amount of Java

Source: Internet
Author: User
Java Amount and Partial conversions:
1. Meta-transfer:
Can be passed in a string or a long (implicitly converted to long or can) integer.
If passed into a string, it is converted by substituting a decimal point, with multiple decimal points or no decimal points considered.
If passed in as long, multiply by 100 by BigDecimal.
2. Sub-transfer element:

Can be passed in a string or a long (implicitly converted to long or can) integer. And make a rough format judgment of the incoming points.

Package com.westsoft.xpf.portal.utils;  

Import Java.math.BigDecimal; /** * com.util.AmountUtils * @description amount Meta Conversion Tool class * @author zcm0708@sina.com * @2012-2-7 pm 12:58:00/Pub Lic class Amountutils {/** amount is a divided format * * public static final String Currency_fen_regex = "\\-?[  
      
    0-9]+ ";  
     /** * Converts a unit into a dollar and returns the amount format of the string (excluding) * * @param amount * @return * @throws Exception */public static String changef2y (Long amount) throws exception{if!amount.tostring (). Matches (Currency_fe  
        N_regex)) {throw new Exception ("Incorrect amount format");  
        int flag = 0;  
        String amstring = amount.tostring ();  
            if (Amstring.charat (0) = = '-') {flag = 1;  
        amstring = amstring.substring (1);  
        } stringbuffer result = new StringBuffer ();  
        if (Amstring.length () ==1) {result.append ("0.0"). Append (amstring); }elseif (amstring.length () = = 2) {result.append ("0.").  
        Append (amstring);  
            }else{String intstring = amstring.substring (0,amstring.length ()-2); for (int i=1; i<=intstring.length (); i++) {if (i-1)%3 = = 0 && i!=1) {RE  
                Sult.append (",");  
            } result.append (Intstring.substring (Intstring.length ()-i,intstring.length ()-i+1)); } result.reverse (). Append (".").  
        Append (Amstring.substring (Amstring.length ()-2));  
        } if (flag = = 1) {return "-" +result.tostring ();  
        }else{return result.tostring (); }/** * Converts the unit into meta (excluding) * * @param amount * @return * @throws Except Ion */public static string changef2y (String amount) throws exception{if (!amount.matches Fen_regex)) {throw new Exception ("The amount format hasMistaken ");  
    Return bigdecimal.valueof (long.valueof (amount)). Divide (new BigDecimal). ToString (); /** * Converts a unit to a divided (by) * * @param amount * @return/public static  
    String changey2f (Long amount) {return bigdecimal.valueof (amount). Multiply (new BigDecimal). ToString (); /** * Converts a unit to a decimal point, supports a comma-differentiated amount * * @param amount * @return * * * PU  Blic static string changey2f (string amount) {String currency = Amount.replaceall ("\\$|\\¥|\\,", "");  
        Processing contains, ¥ or $ amount int index = Currency.indexof (".");  
        int length = Currency.length ();  
        Long Amlong = 0l;  
        if (index = = 1) {Amlong = long.valueof (currency+ "00");  
        }else if (Length-index >= 3) {Amlong = Long.valueof ((currency.substring (0, index+3)). Replace (".", "")); }else if (Length-index = = 2) {AmLOng = Long.valueof ((currency.substring (0, index+2)). Replace (".", "") +0);  
        }else{Amlong = long.valueof ((currency.substring (0, index+1)). Replace (".", "") + "00");  
    return amlong.tostring (); public static void Main (string[] args) {//try {//System.out.println ("Result:" +changef  
2Y (" -000a00"));  
catch (Exception e) {//System.out.println ("----------->>>" +e.getmessage ());  
return E.geterrorcode ();  
          
        }//System.out.println ("Result:" +changey2f ("1.00000000001E10"));  
        System.out.println (amountutils.changey2f ("1.33"));
		try {System.out.println (amountutils.changef2y ("1322"));
		catch (Exception e) {e.printstacktrace ();  
}//System.out.println (Long.parselong (amountutils.changey2f ("1000000000000000")));  
System.out.println (Integer.parseint (amountutils.changey2f ("10000000"))); System.oUt.println (Integer.min_value);  
Long a = 0;  
          
    System.out.println (a);  
 }  
}



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.