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.
You can use a NumberFormat instance.
Or you can use DecimalFormat.
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.