wyoskibum wyoskibum - 1 year ago 347
Android Question

Android: String format with Double value

I need to create a String using the Formater to display some double float values. I'm not clear on how to code it. Here is what I have:

Double dWeightInKg = 100;
Double dWeightInLbs = 220:
String headerText = String.format("%.0f kg / %.0f lbs",Double.toString(dWeightInKg) , Double.toString(dWeightInLbs));

I'm looking for the following output:

100 kg / 220 lbs

I get a runtimeexception - badArgumentType(formater) on my String.format line.

Any suggestions or help would be appreciated. Thanks in advanced!

Answer Source

%.0f is the format string for a float, with 0 decimal places.

The values you're passing to String.format are String, String when it needs to be Double, Double.

You do not need to convert the doubles to strings.

String headerText = String.format("%.0f kg / %.0f lbs", dWeightInKg, dWeightInLbs);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download