Data formatting in Android

Source: Internet
Author: User

1. Decimal number Format tool: DecimalFormat

DecimalFormat df = new DecimalFormat ();d ouble data = 1203.405607809; System.out.println ("Before formatting:" + data); String pattern = "0.0";//1203.4df.applypattern (pattern); System.out.println ("+ pattern +" mode after formatting: "+ df.format (data)),//Can be added to the pattern of any character you want, such as unit pattern =" 00000000.000kg ";// 00001203.406kgdf.applypattern (pattern); System.out.println ("+ pattern +" mode after formatting: "+ df.format (data));//#表示如果存在就显示字符, if not present, can only be used in the pattern of two patterns =" # # 000.000kg ";//1203.406kgdf.applypattern (pattern); System.out.println (after formatting with + pattern + mode: + df.format (data)),//-Indicates that the output is negative and must be placed at the front of the pattern = "000.000";//- 1203.406df.applypattern (pattern); System.out.println ("+ pattern +" mode after formatting: "+ df.format (data));//, is the grouping delimiter: output 12,03.41pattern =" -0,00.0# ";// -12,03.4 1df.applypattern (pattern); System.out.println (after formatting with + pattern + mode: "+ df.format (data)");//E means the output is exponential, the string before "E" is the base format, followed by the exponential format. Pattern = "0.00E000";//1.20e003df.applypattern (pattern); System.out.println (after formatting with + pattern + mode: "+ df.format (data));//%The expression is multiplied by 100 and displayed as a percentage, to be placed in the last pattern = "0.00%";//120340.56%df.applypattern (pattern); System.out.println (After the "+ pattern +" pattern is formatted: "+ df.format (data)"),//"\u2030" means multiplying by 1000 and displayed as a thousand fraction, to be placed in the last pattern = "0.00\u2030" ;//203405.61‰df.applypattern (pattern); System.out.println (after formatting with + pattern + mode: + df.format (data)),//"\U00A4" currency symbol, to be placed on both ends *****1203.41¥pattern = "0.00\ U00A4 ";//1203.41¥df.applypattern (pattern); System.out.println ("+ pattern +" mode after formatting: "+ df.format (data)),//' for special words in the prefix or suffix multibyte quotation marks, to create the single quotation mark itself, use two single quotation marks:" # o " Clock ". pattern = "' # ' #";//#1203//pattern = "' # '";//#1203df. Applypattern (pattern); System.out.println (after formatting with + pattern + mode: "+ df.format (data)");p Attern = "# o ' clock";//1203 o ' Clockdf.applypattern (PA Ttern); System.out.println (After the "+ pattern +" mode is formatted: "+ df.format (data)"),//' placed in the middle or in the back single quotation marks are displayed at the end, placed in the front of the single quotation mark is displayed in the first//pattern = "# o ' C lock.000 ";//1203.406 o ' clock//pattern =" #. 000o "Clock";//1203.406 o ' clock//pattern = "#." ";//1203.406"//Patter n = "#." //1203.406 ' pattern = '' # ';//' 1203.406df.applypattern (pattern); System.out.println ("+ pattern +" mode after formatting: "+ df.format (data));

The result is:

  

2. Date Format tool: SimpleDateFormat

SimpleDateFormat myfmt=new SimpleDateFormat ("YYYY year mm month DD Day hh mm min ss seconds");        SimpleDateFormat myfmt1=new SimpleDateFormat ("Yy/mm/dd hh:mm");         SimpleDateFormat myfmt2=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//equivalent to now.tolocalestring ()        SimpleDateFormat myfmt3=new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec E");        SimpleDateFormat myfmt4=new SimpleDateFormat (                "D-day of the Year in the year of the first W week January the first W week in the day K time Z time zone");        Date Now=new date ();        System.out.println (Myfmt.format (now));        System.out.println (Myfmt1.format (now));        System.out.println (Myfmt2.format (now));        System.out.println (Myfmt3.format (now));        System.out.println (Myfmt4.format (now));        System.out.println (Now.togmtstring ());        System.out.println (Now.tolocalestring ());        System.out.println (Now.tostring ());

The result is:

July 01, 2015 14:13 22 sec 15/07/01 14:132015-07-01 14:13:222015 July 01 14:13 22 second week 31 182th Day of the year 27th Monday month 1th Week in one day 14 o'clock CST Time zone 1 Jul 06:13:22 gmt2015-7-1 14:13:22wed Jul 14:13:22 CST 2015

  

3, similar to the key value pair: Choiceformat

Double[] Limit = {0, 1, 2, 3}; string[] format = {"Hello0", "Hello1", "Hello2", "Hello3"}; Choiceformat CF = new Choiceformat (limit, format), for (int i = 0; i < 4; ++i) {System.out.println (Cf.format (i));}

The result is:

Hello0hello1hello2hello3

  

Data formatting in Android

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.