Problems with float and double conversions in Java

Source: Internet
Author: User
Tags mul

  Why is there no data error for double turn float, but the error of float to double is so great?

double d = 3.14;   float f = (float) D;  System.out.println (f);  The output results are:3.14;   
float f = 127.1f;   double d = F;  System.out.println (d);  Output is:127.0999984741211   
Why is this the result?   How can you avoid this problem and let float go double to get the actual data?  
Workaround: The float type is now converted to a string type, converted to a higher precision bigdecimal type, and then converted to double type.
float f = 127.1f;   New BigDecimal (string.valueof (f));   double d = b.doublevalue ();  System.out.println (d);  

 

/*** provides accurate addition calculation *@paramargs*/      Public Static DoubleAddDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnB1.add (B2). Doublevalue (); }         /*** Provides a precise subtraction calculation * *@paramargs*/      Public Static DoubleSubDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.subtract (B2). Doublevalue (); }         /*** provides accurate multiplication calculation *@paramargs*/      Public Static DoubleMulDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.multiply (B2). Doublevalue (); }         /*** provides (relative) accurate division calculation, when the development of the situation, in addition to be accurate to the * small number of the last 110 digits *@paramargs*/      Public Static DoubleDivDoubleV1,Doublev2) {     returnDiv (v1, v2, Def_div_scale); }         /*** provides (relative) accurate division calculation, when the development of the exception, the scale parameters specified * Precision, after the number four to five in *@paramargs*/      Public Static DoubleDivDoubleV1,DoubleV2,intScale ) {     if(Scale < 0) {     Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.divide (B2, scale, bigdecimal.round_half_up). Doublevalue (); }         /*** Provides a precise small number of four round-off five into the processor *@paramargs*/          Public Static DoubleRoundDoubleVintScale ) {     if(scale<0) {     Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal b=NewBigDecimal (double.tostring (v)); BigDecimal One=NewBigDecimal ("1"); returnb.divide (one, scale, Bigdecimal.round_half_down). Doublevalue (); }          Public Static voidMain (string[] args) {System.out.println (Add (1.2321231, 3.7865765)); System.out.println (Sub (6.4523423, 1.2321231)); System.out.println (Mul (6.4523423, 3.7865765)); System.out.println (Div (6.4523423, 3.7865765, 5)); System.out.println (Round (3.7865765, 5)); }
}
 In Java, float is four bytes, double is eight bytes, float--->double, and if there is no error in the complement, it should be possible.
You first wrap the float type data into BigDecimal data, and then call its Floatvalue () method to implement.

Problems with float and double conversions in Java

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.