3

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

private double convertMetersToFeet(double meters)
{
  //function converts Feet to Meters.
      double toFeet = meters;
      toFeet = meters*3.2808;  // official conversion rate of Meters to Feet
      return toFeet;
}

Problem is the output; for example I get 337.36080000000004 from an input of 101. What's the appropriate practice for truncating the floating points?

As the answers below assumed, I'd want 4 significant figures to remain consistent with my conversion ratio.

GoingTharn
  • 1,134
  • 1
  • 10
  • 19
  • You fail to specify whether you would prefer a result of 337.36080 or 337.3608 or 337.361 or 337.36 or 337.4 or 337. – eleven81 Mar 04 '09 at 17:58
  • Do mean four consistent fractional fictures or a total of four? – Sebastian Ganslandt Mar 04 '09 at 20:04
  • read how double is stored, and why it's impossible to have a well-rounded representation of a decimal number in binary form. – Vladimir Dyuzhev Mar 04 '09 at 20:12
  • FYI: you have five significant figures in your conversion. If you ask for 1 million meters in feet, the correct sig fig answer is 3280800, not 3280000. http://en.wikipedia.org/wiki/Significant_figures#Identifying_significant_digits – Bob Cross Mar 12 '09 at 12:49

5 Answers5

8

You can use a NumberFormat instance.

NumberFormat nf = NumberFormat.getInstance(Locale.UK);
nf.setMinimumFractionDigits(4);
nf.setMaximumFractionDigits(4);
System.out.println(nf.format(feet));

Or you can use DecimalFormat.

DecimalFormat df = new DecimalFormat("0.0000");
System.out.println(df.format(feet));

The latter (DecimalFormat), is to be used when you explicitly want to state the format and the former (NumberFormat), when want localized settings.

For four consistent fractional figures, there is no need to drag in BigDecimal, if your aren't working with really long distances.

Sebastian Ganslandt
  • 997
  • 2
  • 13
  • 26
3

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;
}
GoingTharn
  • 1,134
  • 1
  • 10
  • 19
  • This is incorrect; you should return the calculated value and do the formatting only for output. In part because many decimal fractions cannot be precisely represented as a binary fraction - doing the above, you will still get strange long decimals sometimes. – Lawrence Dol Mar 04 '09 at 21:05
  • While I'd love to agree with you, I'm sort of stuck. I'm manipulating a field that is stored as a double, that is then passed into Oracle Applications. I have no control over the data field's type (double). This will work for most values, and they'll live with the occasional floating point anomaly. – GoingTharn Mar 04 '09 at 21:37
1

If you need precise calculations, use BigDecimal instead of float. If you just want to truncate on printing, use DecimalFormat.

DecimalFormat df = new DecimalFormat ("0.00");
System.out.println(df.format(convertMetersToFeet(101)));
Pesto
  • 23,272
  • 2
  • 67
  • 76
0

Use java.math.BigDecimal for decimal arithmetic.

Tom Hawtin - tackline
  • 139,906
  • 30
  • 206
  • 293
0

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.

Community
  • 1
  • 1
Sebastian Ganslandt
  • 997
  • 2
  • 13
  • 26