Detailed description of DecimalFormat rounding in java,

Source: Internet
Author: User

Detailed description of DecimalFormat rounding in java,

DecimalFormat is a subclass of NumberFormat used to format decimal numbers. It supports different types of numbers, including INTEGER (123), fixed point (123.4), Scientific notation (1.23E4), percentage (12%), and amount ($123) the content is localized.

The following describes the usage of DecimalFormat:

Import java. text. *; import java. util. *; public class DecimalFormatDemo {public static void main (String args []) {DecimalFormat df1 = new DecimalFormat ("###,###. 0000 "); // use the default System format. out. println (df1.format (111111123456.12); Locale. setDefault (Locale. US); DecimalFormat df2 = new DecimalFormat ("###,###. 0000 "); // use the US format System. out. println (df2.format (111111123456.12); // ---------------------------- also use applypattern values // DecimalFormat df3 = new DecimalFormat (); myformat3.applyPattern ("##,###. 000 "); System. out. println (df3.format (11112345.12345); // ----------------- control index output; // DecimalFormat df4 = new DecimalFormat (); myformat4.applyPattern ("0.000E0000"); System. out. println (df4.format (10000); System. out. println (df4.format (12345678.345); // -------------------- percentage output --------------------------------------------- // DecimalFormat df5 = null; try {df5 = (DecimalFormat) NumberFormat. getPercentInstance ();} catch (ClassCastException e) {<span style = "white-space: pre"> </span> System. err. println (e);} df5.applyPattern ("00.0000%"); System. out. println (df5.format (0.34567); System. out. println (df5.format (1.34567 ));}}

(1) Rounding data:

DecimalFormat contains a group of symbols. The meanings of these symbols are described as follows:

0: a number

# A number, excluding 0

. Placeholder for decimal separator

, Group separator placeholder

; Separator format.

-Default negative prefix.

% Multiplied by 100 and displayed as Percentage

? Multiply by 1000 and display it as a kilobytes of currency; replace it with a currency symbol; replace it with an international currency symbol if you double write. If it appears in a mode, the decimal separator is used as the decimal separator.

Any other character used in the X prefix or suffix to reference special characters in the prefix or suffix.

For example:

DecimalFormat df1 = new DecimalFormat ("### 0.00"); // retain two decimal places. If less than two decimal places exist, the System is automatically set to zero. out. println (df1.format (124.367); System. out. println (df1.format (124.3); DecimalFormat df2 = new DecimalFormat ("### 0. # "); // retain two decimal places. If there are less than two decimal places, the System is not supplemented. out. println (df2.format (124.6); System. out. println (df2.format (124); DecimalFormat df3 = new DecimalFormat ("000.000"); // retain three decimal places. If the number is not enough, add System. out. println (df3.format (24); DecimalFormat df = new DecimalFormat ("0.000E0000"); // index System. out. println (df. format (1234.56); DecimalFormat nf = (DecimalFormat) NumberFormat. getPercentInstance (); // percentage System. out. println (nf. format (0.476354); nf. applyPattern ("00.00%"); System. out. println (nf. formats (0.476354 ));

Running result:

124.37 124.30 124.6 124 024.000 1.235E0003 48% 47.64% 

(2) For reading and parsing strings containing formatted numbers? Resolution can be included in NumberFormat. For example:

Import java. util. locale; import java. text. numberFormat; import java. text. parseException; public class DecimalFormat5 {public static void main (String args []) {// local format NumberFormat nf1 = NumberFormat. getInstance (); Object obj1 = null; // format-based parsing try {obj1 = nf1.parse ("1234,56");} catch (ParseException e1) {System. err. println (e1);} System. out. println (obj1); // German format NumberFormat nf2 = NumberFormat. getInstance (Locale. GERMAN); Object obj2 = null; // format-based parsing try {obj2 = nf2.parse ("1234,56");} catch (ParseException e2) {System. err. println (e2);} System. out. println (obj2 );}}

Running result:

123456 // run in the United States; considered string 1234.56 // run in Germany; Considered as a decimal

(3) For DecimalFormat and NumberFormat:

DecimalFormat is a subclass of NumberFormat. Its instance is specified as a specific region. Therefore, you can use NumberFormat. getInstance to specify a region and forcibly convert the structure to a DecimalFormat object. This technology can be applied in most cases, however, you need to use try/catch Block to enclose the forced conversion to prevent the conversion from working properly (probably using a strange region in a very unknown situation ).

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.