Display number in Textview with thousand separator and decimal format
Asked Answered
T

2

7

How can I format, according to Locale.getDefault() of device, a float greater than 999.

For decimal format currently I'm using this:

DecimalFormat decim = new DecimalFormat("#.##");
tv.setText(decim.format(someFloat));

And for thousand separator:

tv.setText(String.format(Locale.getDefault(), "%,d", someInt));

How can I combine both if I want to display 3.678,66 (or 3,678.66 - depending of Locale.getDefault())?

Truffle answered 4/3, 2018 at 13:26 Comment(1)
This has an answer (not accepted) here: #26129026. Couldn't find that before posting my question.Truffle
T
17

This did the trick:

DecimalFormat decim = new DecimalFormat("#,###.##");
tv.setText(decim.format(someFloat));
Truffle answered 4/3, 2018 at 15:9 Comment(0)
R
1

You can try

NumberFormat.getInstance().format(my number)

to format to default locale

Rabelais answered 4/3, 2018 at 13:31 Comment(1)
Will this limit display to two decimals places, as in the example I posted? I want to format to default locale, but as in the code shown - two decimal places max, only if necessary (ie. not 3.678,00).Truffle

© 2022 - 2024 — McMap. All rights reserved.