Java reserved two-bit decimals and JS reserved two-bit decimal consistency study

Source: Internet
Author: User
Tags pow java decimalformat

One, Java reserved two-bit decimal way

 Public Static voidMain (string[] args) {System.out.println ("=======decimalformat (rounding, five into possible failure) ========="); DecimalFormat DecimalFormat=NewDecimalFormat ("0.00"); System.out.println (Decimalformat.format (0.235));//0.23System.out.println (Decimalformat.format (1.235));//1.24System.out.println ("=======string.format (rounded) ========="); System.out.println (String.Format ("%.2f", 0.235));//0.24System.out.println (String.Format ("%.2f", 1.235));//1.24System.out.println ("=======numberformat (rounding, five into possible failure) ========="); NumberFormat NF=numberformat.getnumberinstance (); Nf.setmaximumfractiondigits (2); System.out.println (Nf.format (0.235));//0.23System.out.println (Nf.format (1.235));//1.24System.out.println ("=======bigdecimal (Specify rounding mode, constructors use string type to calculate accurately) ========="); System.out.println ("Round_half_down: Five five Points 1 in."); //BigDecimal BigDecimal = new BigDecimal (1.235);//use the double type directly to calculate the wrong//System.out.println (Bigdecimal.setscale (2,bigdecimal.round_half_down). Doublevalue ());BigDecimal BigDecimal=NewBigDecimal (String.valueof (0.235)); System.out.println (Bigdecimal.setscale (2,bigdecimal.round_half_down). Doublevalue ());//0.23BigDecimal=NewBigDecimal (1.235+ ""); System.out.println (Bigdecimal.setscale (2,bigdecimal.round_half_down). Doublevalue ());//1.23BigDecimal=NewBigDecimal (1.2351+ ""); System.out.println (Bigdecimal.setscale (2,bigdecimal.round_half_down). Doublevalue ());//1.24System.out.println ("Round_half_up: Rounding"); //bigDecimal = new BigDecimal (0.235);//use the double type directly to calculate the wrong//System.out.println (Bigdecimal.setscale (2,bigdecimal.round_half_up). Doublevalue ());BigDecimal=NewBigDecimal (String.valueof (0.235)); System.out.println (Bigdecimal.setscale (2,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ());//0.24BigDecimal=NewBigDecimal (1.235+ ""); System.out.println (Bigdecimal.setscale (2,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ());//1.24}

Results

=======decimalformat (rounded, five into possible failure) =========0.231.24=======string.format (rounded) =========0.241.24======= NumberFormat (rounding, five into possible failure) =========0.231.24=======bigdecimal (Specify rounding mode, constructors use string type to calculate accurately) =========round_half_ Down: Five five points 1 into 0.231.231.24round_half_up: Rounding 0.241.24

Second, JS reserved two decimal places

0.235.toFixed (2); 0.231.235.toFixed (2); 1.24

Summarize:

1.js.tofixed Way and Java DecimalFormat and numberformat the same way, the results are uncertain, four five may not enter!

2.java can be accurate 455 into the String.Format mode and the BigDecimal mode of ROUND_HALF_UP, but you must ensure that the BigDecimal construction parameter is a string type, otherwise there will be five phenomenon.

3.js exact Rounding implementation method, the online find example, as follows

function (fractiondigits)  {      // does not do any processing on fractiondigits, assuming it is a  valid input    return ( parseint (this * MATH.POW (Ten, fractiondigits  ) + 0.5)/math.pow (10,fractiondigts)). toString ();    

Therefore, if the front and rear table calculation is consistent, there are the following scenarios

Front Background Results
ToFixed DecimalFormat or NumberFormat Inaccurate rounding, but consistent results
The improved tofixed BigDecimal using ROUND_HALF_UP mode Exactly 455, and the results are consistent.

Because of the diversity of browsers, the infinite nature of the data, not too much testing, this is only a preliminary conclusion. It is best to make sure that it is foolproof or a computational party!

Java reserved two-bit decimals and JS reserved two-bit decimal consistency study

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.