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

前端 未结 5 512
爱一瞬间的悲伤
爱一瞬间的悲伤 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 17:06

    I'm answering my own question for posterity's sake. I used the DecimalFormat answer above, but the answers failed to take into account the return type of the method.

    Here's the finished code:

      private double convertMetersToFeet(double meters)
    {
      //function converts Feet to Meters.
          double toFeet = meters;
          toFeet = meters*3.2808;  // official conversion rate of Meters to Feet
          String formattedNumber = new DecimalFormat("0.0000").format(toFeet); //return with 4 decimal places
          double d = Double.valueOf(formattedNumber.trim()).doubleValue();
          return d;
    }
    

提交回复
热议问题