Java floating point math - (conversion for feet/meters)

前端 未结 5 531
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 16:36

Pretty basic question I think - I\'m performing this function:

private double convertMetersToFeet(double meters)
{
  //function converts Feet to Meters.
             


        
5条回答
  •  醉酒成梦
    2021-01-20 16:59

    If it is not for printing you should not care about the error. 337.36080000000004 is just as correct as 337.3608 since you only have 5 significant digits in the factor and only 3 in the test input. (And I certainly hope that the answer your method gave was 331 and not 337)

    However, this line fetched from another question seems to do the trick as well.

    double toFeet = ((int)(meters*3.2808*10000))/10000.0;
    

    The only problem is that is overflows a lot faster.

提交回复
热议问题