Java.text.NumberFormat

Source: Internet
Author: User

NumberFormatis the abstract base class for all numeric formats. This class provides an interface for formatting and parsing numeric values. NumberFormatIt also provides methods to determine which locales have numeric formatting, and what their names are.

NumberFormatNumeric values that can be used to format and parse any locale. Enables the code to be completely independent of the locale conventions of the decimal point, thousands separator, or even the specified number of decimal places, regardless of whether the numeric format is even decimal.

To format the values of the current Locale, you can use one of the factory class methods:

  

If you format multiple numeric values, it is more efficient to get the format and use it multiple times, so that the system does not have to get information about the language and country/region conventions more than once.

NumberFormat NF = numberformat.getinstance (); for (int i = 0; i < mynumber.length; ++i) {     

To format a date for a different Locale, you can specify it in the invocation of the pair getInstance .

 
 

You can also use NumberFormat to parse values:

MyNumber = Nf.parse (myString);

Import Java.math.roundingmode;import Java.text.decimalformat;import Java.text.numberformat;public class TestClass {/  * * @param args */public static void main (string[] args) {Double mynumber=23323.3325632323;  Double test=0.3464;  getinstance ()//returns the general numeric format of the current default locale.  String myString = numberformat.getinstance (). Format (MyNumber);    System.out.println (myString);  Getcurrencyinstance () returns the currency format of the current default locale.   myString = Numberformat.getcurrencyinstance (). Format (MyNumber);    System.out.println (myString);  Getnumberinstance () returns the general numeric format for the current default locale.   myString = Numberformat.getnumberinstance (). Format (MyNumber);    System.out.println (myString);  Getpercentinstance () returns the percentage format for the current default locale.   myString = Numberformat.getpercentinstance (). Format (test);    System.out.println (myString);    NumberFormat format = Numberformat.getinstance ();  setminimumfractiondigits (int) Sets the minimum number of digits allowed for the decimal part of a numeric value.    Format.setminimumfractiondigits (3);  setmaximumfractiondigits (int) Sets the maximum number of digits allowed for the decimal portion of a number. Format.setmaximumfRactiondigits (5);   setmaximumintegerdigits (int) Sets the maximum number of digits allowed for an integer portion of a numeric value.    Format.setmaximumintegerdigits (10);  setminimumintegerdigits (int) Sets the minimum number of digits allowed for an integer portion of a numeric value.    Format.setminimumintegerdigits (1);  Set the Roundingmode used in this numberformat.    Format.setroundingmode (ROUNDINGMODE.HALF_UP); System.out.println (Format.format (0.2321222));}}

Java.text.NumberFormat

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.