The double type is converted to an integer type---decimalformat

Source: Internet
Author: User

Suppose X is the double type variable you want to convert:

No rounding operation: (int) x

Rounding Operation: Integer.parseint (New Java.text.DecimalFormat ("0"). Format (x));

DecimalFormat is a specific subclass of NumberFormat that is used to format decimal digits. The class has a variety of features that enable it to analyze and format numbers in any locale, including support for Western, Arabic, and Indic numerals. It also supports different types of numbers, including integers (123), fixed-point numbers (123.4), numbers represented by scientific notation (1.23E4), percentages (12%) and amount ($123). All of this content can be localized.

DecimalFormat contains a pattern and a set of symbols

Symbolic Meaning:

Symbol location localization? Meaning
0 numbers are Arabic numerals
# numbers are Arabic numerals, and if they don't, they appear empty.
. Number is a decimal separator or currency decimal separator
-Number is minus
, the number is a grouping separator
The E number is the mantissa and exponent in the separation science notation. No quotation marks are required in the prefix or suffix.
; Child mode boundaries are separated by positive and negative sub-patterns
The% prefix or suffix is multiplied by 100 and displayed as a percentage
/u2030 prefix or suffix is multiplied by 1000 and displayed as a thousand fraction
¤ (/u00a4) prefix or suffix no currency notation, replaced by currency symbol. If two appears simultaneously, it is replaced with an international currency symbol. If it appears in a pattern, the currency decimal separator is used instead of the decimal separator character.
' Prefix or suffix no is used to multibyte quotation marks for special words in a prefix or suffix, such as ' # ' # 123 formatted as ' #123 '. To create a single quotation mark itself, use two single quotes consecutively: "# o ' Clock".

Example:

[Java] View plain copy

DecimalFormat df1 = new DecimalFormat ("0.0");
DecimalFormat DF2 = new DecimalFormat ("#.#");
DecimalFormat df3 = new DecimalFormat ("000.000");
DecimalFormat df4 = new DecimalFormat ("###.###");
System.out.println (Df1.format (12.34));
System.out.println (Df2.format (12.34));
System.out.println (Df3.format (12.34));
System.out.println (Df4.format (12.34));

Operation Result:
12.3
12.3
012.340
12.34

[Java] View plain copy

DecimalFormat format = new DecimalFormat ("###,####.000");
System.out.println (Format.format (111111123456.1227222));

Locale.setdefault (locale.us);
DecimalFormat Usformat = new DecimalFormat ("###,###.000");
System.out.println (Usformat.format (111111123456.1227222));

DecimalFormat Addpattenformat = new DecimalFormat ();
Addpattenformat.applypattern ("##,###.000");
System.out.println (Addpattenformat.format (111111123456.1227));

DecimalFormat Zhiformat = new DecimalFormat ();
Zhiformat.applypattern ("0.000E0000");
System.out.println (Zhiformat.format (10000));
System.out.println (Zhiformat.format (12345678.345));

DecimalFormat Percentformat = new DecimalFormat ();
Percentformat.applypattern ("#0.0%");
System.out.println (Percentformat.format (0.3052222));

Operation Result:
1111,1112,3456.123
111,111,123,456.123
111,111,123,456.123
1.000E0004
1.235E0007
30.522%
If you use a pattern with more than one grouping character, the interval between the last delimiter and the end of the integer is the grouping size used. So "#,##,###,####" = = "######,####" = = "##,####,####".

The double type is converted to an integer type---decimalformat

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.