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

AndroidStringAndroid ResourcesFormatter

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

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

Solution 3 - Android

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 - Android

A simpler approach:

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

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

Output: 3.15

Solution 5 - Android

This worked for me.

<string name="market_price">Range ₹%1$.0f - ₹%2$.0f</string>
android:text="@{@string/market_price(viewModel.suggestedPriceRange.max, viewModel.suggestedPriceRange.min)}"

Outputs: Range ₹500 - ₹1000

In ₹%1$.0f, .0f defines how many digits you want after the decimal.

Solution 6 - Android

If it were me I'd store the values in the resources as simple values, and then use formatter methods to control how they're displayed, roughly like this

public String formatFigureTwoPlaces(float value) {
    DecimalFormat myFormatter = new DecimalFormat("##0.00");
    return myFormatter.format(value);
}

public String formatFigureOnePlace(float value) {
    DecimalFormat myFormatter = new DecimalFormat("##0.0");
    return myFormatter.format(value);
}

Solution 7 - Android

I now that this reply is arriving too late... but I hope to be able to help other people:

Android sucks with multiple parameters substitutions when you want decimal numbers and format this in common style %a.bf

The best solution I have found (and only for these kind of resources) is put the decimal parameters as strings %n$s and in the code apply my conversion with String.format(...)



Example:


INCORRECT WAY:

// In xml file:

<string name="resource1">You has a desviation of %1$s and that is a %2$.2f%% percentage.</string>

// And in java file

  String sresult = getString(R.string.resource1, svalue, spercentage); // <-- exception!

This solution is technically correct but incorrect due to android substitution resources system so the last line will generate an exception.



CORRECT WAY / SOLUTION:

Simply convert the second parameter into a String.

<string name="resource1">You has a desviation of %1$s and that is a %2$s percentage.</string>

And now in the code:

...

  // This is the auxiliar line added to solve the problem
  String spercentage = String.format("%.2f%%",percentage);

  // This is the common code where we use the last variable.
  String sresult = getString(R.string.resource1, svalue, spercentage);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlomzaView Question on Stackoverflow
Solution 1 - AndroidNik ReimanView Answer on Stackoverflow
Solution 2 - AndroidWael OuniView Answer on Stackoverflow
Solution 3 - AndroidAditya RohillaView Answer on Stackoverflow
Solution 4 - AndroidGenieView Answer on Stackoverflow
Solution 5 - AndroidOhhhThatVarunView Answer on Stackoverflow
Solution 6 - AndroidOllie CView Answer on Stackoverflow
Solution 7 - AndroidDavid AiramView Answer on Stackoverflow