2

My target is to show text like "1 star" "2.5 stars" "3 stars".

which is remove .0 for float value like 1.0, 2.0 etc. But for the value with .5 show the float value 2.5 3.5 etc.

Is this possible to use android plurals ?

I used this way but doesn't work. plurals.xml

<plurals name="hotel_card_rating_text">
        <item quantity="zero">@string/text_zero</item>
        <item quantity="one">@string/text_one</item>
        <item quantity="two">@string/text_two</item>
        <item quantity="few">@string/text_few</item>
        <item quantity="many">@string/text_many</item>
        <item quantity="other">@string/text_other</item>
    </plurals>

strings.xml

<resources>
    <string name="text_zero">%.1f stars</string>
    <string name="text_one">1 star</string>
    <string name="text_two">%.1f stars</string>
    <string name="text_few">%.1f stars</string>
    <string name="text_many">%.1f stars</string>
    <string name="text_other">%.1f stars</string>
</resources>

test code

 DecimalFormat format = new DecimalFormat();                    
    float rating = getRating();
    getStarText().setText(getContext().getResources().getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
            }
William Hu
  • 15,423
  • 11
  • 100
  • 121

2 Answers2

1

you can give it a try to below snippet which is very typical

String somePostFixText = "star";
String output;
double starCount;

if (starCount > (double)1)
      somePostFixText = somePostFixText+"s";

if(starCount == (long) starCount){
       output = String.format("%d",(long)starCount)+" "+somePostFixText;
} else {
       output = String.format("%s",starCount)+" "+somePostFixText;
}

//do whatever you want to do with output variable

Happy Coding!

Shrenik Shah
  • 1,900
  • 1
  • 11
  • 19
  • 1
    ahh..! okay i guess above code will work with your mechanism too just a little changes by converting values into static resources you can achieve what you want. – Shrenik Shah Sep 26 '16 at 06:31
1

strings.xml use %s instead of %.1f

<resources>
    <string name="text_zero">%s stars</string>
    <string name="text_one">1 star</string>
    <string name="text_two">%s stars</string>
    <string name="text_few">%s stars</string>
    <string name="text_many">%s stars</string>
    <string name="text_other">%s stars</string>
</resources>

test code

DecimalFormat format = new DecimalFormat("#.#"); // format the number
float rating = getRating();
getContext().getResources()
    .getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));

Be careful when you use plurals. It has its own issues, see below:

Plural definition is ignored for zero quantity

Issue 8287: PluralRules does not handle quantity "zero"

Community
  • 1
  • 1
Trey Cai
  • 1,195
  • 1
  • 12
  • 18
  • 2
    The `zero`quantity doesn't have issues - it's just a common misunderstanding of how the quantities work and what the purpose of them is. For example, `zero`, `one` and `two` does not map directly to the exact numbers 0, 1 and 2 respectively, and as such they are not used by all languages (even though the numbers obviously can be used by all languages). – LoPoBo Jan 27 '17 at 12:14