The difference between the Double class parsedouble () and the valueof () method

Source: Internet
Author: User

String literals of numeric types are converted to floating-point numbers usually using the parsedouble () and valueof () methods.

There are two differences between the two main points.

Difference One: Parameter difference
Double.parsedouble (java.lang.String) parameter can only be String if the argument is changed to a double type hint "the method parsedouble (String) in the type double Is isn't applicable for the arguments (double) "error.
The parameter types for double.valueof () can be floating-point or string-capable.

1  Public voidTesttem () {2System.out.println ("==================");3String pi = "3.1415926";4Double doubleValue1 =double.valueof (pi);5Double doubleParse1 =double.parsedouble (pi);6System.out.println (String.Format ("doubleValue1 =%s, DoubleParse1 =%s", DoubleValue1, doubleParse1));7 8Double piofdouble = 3.1415926;9Double doubleValue2 =double.valueof (piofdouble);TenDouble DoubleParse2 =double.parsedouble (piofdouble.tostring ()); OneSystem.out.println (String.Format ("doubleValue2 =%s, DoubleParse2 =%s", DoubleValue2, DoubleParse2)); A  -Float pioffloat =float.valueof (pi); -Double DoubleValue3 =double.valueof (pioffloat); theDouble DOUBLEPARSE3 =double.parsedouble (pioffloat.tostring ()); -System.out.println (String.Format ("DoubleValue3 =%s, DoubleParse3 =%s", DoubleValue3, doubleParse3)); -System.out.println ("=================="); -}


Console output Results:

1 ==================2 doubleValue1 = 3.1415926, doubleParse1 = 3.14159263 doubleValue2 = 3.1415926, DoubleParse2 = 3.14159264 doubleValue3 = 3.141592502593994, doubleParse3 = 3.14159255 ===== =============


Difference Two: return type
Double.parsedouble (java.lang.String) converts a string of numeric type to a double type
Double.valueof () to convert a string of numeric type to a double type
Source code for the Double.parsedouble () method:

     Public Static Double throws numberformatexception {            return  floatingdecimal.readjavaformatstring (s). Doublevalue ();    } 


Source code for the Double.valueof () method:

     Public Static throws numberformatexception {        //  new Double ()            returnNew  Double (floatingdecimal.readjavaformatstring (s). Doublevalue ());    }



By formatting the number of decimal places the result is the same, you can see that the difference between the parameter and the return type is small.

    New DecimalFormat ("0.00");    System.out.println (Df.format (double.parsedouble (PI)));    System.out.println (Df.format (double.valueof (PI)));


Console output Results:

    3.14    3.14



Ref: 78115171

The difference between the Double class parsedouble () and the valueof () method

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.