1

Based on the user-locale I show decimals, for example: 1234.567 will be in:

  • US: 1,234.567
  • NL: 1.234,567

Therefor I use: NumberFormat.decimalPattern(locale.toString()).format(1234.567)

That works fine :)

But now I want to set the max-digets. There is a maximumFractionDigits, but how to apply this one?

This one doesn't work: NumberFormat.decimalPattern(locale.toString(), maximumFractionDigits: 2).format(1234.567)

On the currency it works like this: NumberFormat.currency(locale: locale.toString(), decimalDigits: 3).format(1234567)

1
  • Can you put an example of the desired output? Commented Aug 27, 2020 at 10:28

1 Answer 1

3

From another question:

var forFractions = new NumberFormat();

forFractions.minimumFractionDigits = 2;
forFractions.maximumFractionDigits = 2;

format(num n) => forFractions.format(n);

print(format(15.501234));

So, create your format and set the maximumFractionDigits attribute then execute the format.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Of course it should be this way. Just to add. If you want to add the locale to the number then initialize it like: NumberFormat numberFormat = new NumberFormat.decimalPattern(locale.toString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.