Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a double formatted as a price in a textview in android

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?

like image 943
adrian4aes Avatar asked Dec 20 '12 16:12

adrian4aes


People also ask

Can you have multiple styles inside TextView?

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().

What is the return type of getText in android?

GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field.


2 Answers

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.

like image 182
azertiti Avatar answered Nov 03 '22 02:11

azertiti


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.

like image 28
raukodraug Avatar answered Nov 03 '22 00:11

raukodraug