我的目标是显示“1星”“2.5星”“3星”等文本。Android显示整数和浮点数的复数
是删除.0
浮法价值像1.0, 2.0 etc.
但对于.5
值显示浮点值2.5 3.5 etc.
这是可能使用Android plurals
?
我用这种方式,但不起作用。 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>
测试代码
DecimalFormat format = new DecimalFormat();
float rating = getRating();
getStarText().setText(getContext().getResources().getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
}
“零”量不存在问题 - 这只是对数量如何工作以及它们的目的是什么的常见误解。例如,“零”,“一”和“两”不直接映射到确切的数字0,1和2,因此它们不被所有语言使用(即使数字显然可以被所有语言使用语言)。 – LoPoBo