-2

I would like to know how can I show in my TextView the price as follows:

Price is a double value. a=8.75 and b=9.00

I need display a=8.75 and b=9.

I supose I need to check something and change its value. But I dont know what.

Please, help me. Thank you!

Héctor Prats
  • 273
  • 3
  • 14

3 Answers3

2

Representing money in Java is quite tricky. Use NumberFormat, and specifically the implementation returned by getCurrencyInstance() method. You can adjust the number of fraction digits to display using setMinimumFractionDigits() and setMaximumFractionDigits(). Avoid storing money values in double, use BigDecimal to ensure you don't lose precision when making money calculations. There are numerous topics available on the Internet that explain why you should be extra careful when dealing with money in Java, this one looks to be pretty helpful.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • You're welcome! Please don't forget to accept the answer in case you found it helpful. – Egor Apr 30 '15 at 20:46
1
double num; //eg 9.00 or 8.75
long beforeDecimal = (long)num;
double afterDecimal = num - beforeDecimal;
if(afterDecimal>0)
display num;
else
display beforeDecimal;
abhishesh
  • 3,246
  • 18
  • 20
0

from what i understood from your question was that you want to display the following two texts in a TextView : "8.75" and "9".

If thats the case then set the text to your TextView like this:

double priceA = 8.75;
double priceB = 9;
String s = ""+priceA; // or priceB
tv.setText(s);
Izak
  • 909
  • 9
  • 24