I want to show in a textView a price value like: 12,199.99 in a textview in Android. but I have the value stored in a double variable (12199.99). Is there any way to show that double in the textview in the format I want?
There are many ways to make multiple styles inside android TextView and in this example I have made multiple style with three different ways using HTML tags and Html. fromHtml().
GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field.
If you want to format it using the phone's locale with 2 decimals do like this:
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String formattedValue = decimalFormat.format(yourDoubleValue);
yourTextView.setText(formattedValue);
Hope it helps.
You can use a BigDecimal
for it, for example
public static BigDecimal doubleToScale(double d, int scale){
return new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP);
}
I hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With