Double parameter with 2 digits after dot in strings.xml?

45,804

Solution 1

Just adding to @David Airam's answer here; the "incorrect" solution he gives is actually correct, but with a bit of tweaking. The XML file should contain:

<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>

Now in the Java code:

String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);

The exception that @David Airam reported is from trying to jam a String into a format specifier with %f, which requires a floating point type. Use float and there is no such exception.

Also, you can use Float.valueOf() to convert a String to a float in case your input data was originally a string (say, from a EditText or something). However, you should always try/catch valueOf() operations and handle the NumberFormatException case, since this exception is unchecked.

Solution 2

%.1f work for me if you like to show only 1 digit after ','

Solution 3

Define is strings.xml file

  <string name="price_format">$%,.2f</string>

//For using in databinding  where amount is double type
    android:text="@{@string/price_format(model.amount)}"

//For using in java runtime where priceOfModifier is double type
                amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));

Solution 4

A simpler approach:

<string name="decimalunit">%.2f%n</string>

float sfloat= 3.1475926; String sresult = getString(R.string.decimalunit, sfloat);

Output: 3.15

Share:
45,804
lomza
Author by

lomza

I'm an Android developer. You can check my tutorials and post about useful things, programming and other stuff on my blog http://lomza.totem-soft.com

Updated on August 24, 2021

Comments

  • lomza
    lomza over 2 years

    I want to have a parameter in one string in strings.xml and this parameter should be a double value. So I use %1$f. Here - http://developer.android.com/reference/java/util/Formatter.html there are many examples, but what if I want to have have a few double/float parameters and I want only the second one to have 2 digits after .? I tried to use combinations like %2$.2f or %2.2$f. Nor of them worked. %.1f does not work as well. So, does anybody know how can I "customize" a float/double value inside a strings.xml? Thanks.

  • accordionfolder
    accordionfolder about 10 years
    The comment here is the real answer! You should put it as another answer to this question. I almost didn't see it.
  • Kachi
    Kachi over 9 years
    the answer by Iomza in the comments is the real solution
  • Magnus
    Magnus over 7 years
    What is the type of spercentage in your "incorrect" example? I'm guessing (as @Nik Reiman points out) that it's a String - i.e. no wonder you're getting an exception when trying to pass it as a float (at %2$.2f). This "answer" is incomplete and misleading.
  • Aaron
    Aaron over 6 years
    I also can't get formatted floats to work from a string resource, and yes I'm passing a float. I use "%1$0.6f" in the string resource, and I get an exception: java.util.MissingFormatWidthException: %01$.6f so it's getting mangled somehow. This answer is correct.