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:

  • click effect on button in Android
  • easiest way to debug crash in native library, linked by Android app?
  • How to get Exposure Compensation level from Android phone camera, via Java, when picture is taken?
  • android: how do I check if dialogfragment is showing
  • is there a way to disable hardware acceleration only for android 4.0.3?
  • High CPU usage with Android emulator (qemu-system-i386.exe)
  • 100 kg / 220 lbs
    

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

    Related posts:

    How to Change color of Button in Android when Clicked?
    HttpUrlConnection.openConnection fails second time
    Flash in an Android WebView - layering issue
    View.setPadding accepts only in px, is there anyway to setPadding in dp?
    How to play video using FFMPEG library in Android?
    SoLoader$WrongAbiError with React Native Android
  • What does LayoutInflater in Android do?
  • Android: Fragments backStack
  • Working example of using Twitter API on Android?
  • apktool is not generating apk file
  • I can't install intel HAXM
  • Android 4.2 broke my encrypt/decrypt code and the provided solutions don't work
  • 3 Solutions collect form web for “Android: String format with Double value”

    %.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);
    

    This should work,

    DecimalFormat df = new DecimalFormat("#.##");
    private String convertToFormat(double value){
    
        return df.format(value);
    }
    

    You don’t need the Double.ToString() since your formatter is already expecting a number. Try this:

    String headerText = String.format("%.0f kg / %.0f lbs", dWeightInKg , dWeightInLbs);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.