Java. text. NumberFormat

Source: Internet
Author: User

Java. text. NumberFormat

NumberFormatIs the abstract base class of all numeric formats. This class provides interfaces for formatting and parsing numeric values.NumberFormatSome methods are provided to determine which language environments have numerical formats and what their names are.

NumberFormatIt can be used to format and parse numeric values in any language environment. This allows the code to be completely independent from the language conventions of the decimal point, thousands of delimiters, or even specific decimal places, and has nothing to do with whether the numeric format is an even decimal.

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

  myString = NumberFormat.getInstance().format(myNumber); 

If you format multiple values, it is more efficient to obtain the format and use it multiple times, in this way, the system does not have to obtain information about language and environment languages and national/regional agreements for multiple times.

 NumberFormat nf = NumberFormat.getInstance(); for (int i = 0; i < myNumber.length; ++i) {     output.println(nf.format(myNumber[i]) + "; "); } 

To format the date of different Locale, you cangetInstanceIt is specified in the call.

 NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); 

You can also useNumberFormatTo parse the value:

 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 language environment. String myString = NumberFormat. getInstance (). format (myNumber); System. out. println (myString); // getCurrencyInstance () returns the currency format of the current default language environment. MyString = NumberFormat. getCurrencyInstance (). format (myNumber); System. out. println (myString); // getNumberInstance () returns the general value format of the current default language environment. MyString = NumberFormat. getNumberInstance (). format (myNumber); System. out. println (myString); // getPercentInstance () returns the percentage format of the current default language environment. MyString = NumberFormat. getPercentInstance (). format (test); System. out. println (myString); NumberFormat format = NumberFormat. getInstance (); // setMinimumFractionDigits (int) sets the minimum number of digits allowed in the decimal part of the value. Format. setMinimumFractionDigits (3); // setMaximumFractionDigits (int) sets the maximum number of digits allowed in the fractional part of the value. Format. setMaximumFractionDigits (5); // setMaximumIntegerDigits (int) sets the maximum number of digits allowed for the integer part of the value. Format. setMaximumIntegerDigits (10); // setMinimumIntegerDigits (int) sets the minimum number of digits allowed in the integer part of the value. format. setMinimumIntegerDigits (1); // set the RoundingMode used in this NumberFormat. Format. setRoundingMode (RoundingMode. HALF_UP); System. out. println (format. format (0.2321222 ));}}

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.