FormattedNumber
を使用して小数点を処理します。複数ため
it('formats numbers with decimal separators',() => {
const el = <FormattedNumber value={0.1} minimumFractionDigits={2} />;
renderer.render(el, intlProvider.getChildContext());
expect(renderer.getRenderOutput()).toEqualJSX(
<span>0.10</span>
);
});
利用異なる値:
it('pluralizes labels in strings',() => {
const el = (
<FormattedMessage
id="num_emails"
defaultMessage="You have 1.{teaspoons, plural, one {# teaspoons} other {# teaspoons}}."
values={{
teaspoons: 2,
}}
/>
);
と特異:例えば
it('pluralizes labels in strings',() => {
const el = (
<FormattedMessage
id="num_emails"
defaultMessage="You have 2.{teaspoons, plural, one {# teaspoons} other {# teaspoons}}."
values={{
teaspoons: 1,
}}
/>
);
参照
ああ、それは私が求めていたものではないのです!私の質問は10進数を扱う方法ではなく、10進数に応じて正しい形式の名詞を表現できるかどうかについてでした(私の引用例のteaspoon/teaspoons参照)。 – azangru
あなたの編集した答えにあなたが示唆しているアプローチは間違いなく面白いです!私はそれを考えなかった。ただし、この方法では、渡す数値は常に小数点以下(小数点の前の数字はハードコードされている)であることが前提です。しかし、より一般的な方法はありますか?それは "1ティースプーン"、 "2ティースプーン"、 "1.1ティースプーン"、 "1.2ティースプーン"のケースを扱うでしょうか? – azangru
'switch/case'ステートメントのように動作する[selectFormat](https://youtu.be/4ZXagCR9urg?t=11m58s)があります。 [テストケース](https://github.com/yahoo/intl-messageformat/blob/master/tests/benchmark/new_complex_msg_preparsed.js)と[チュートリアル](https://medium.com/@tomi)があります。 .trescak/setting-up-i18n-with-react-intl-in-mantra-react-application-12f90ff34f17)がどのように動作するかを示しています。 –