显示价格 - 根据其在 TextView 中的值加倍为整数 - Android
Show price - double as Integer depending on its value in TextView - Android
我想知道如何在我的 TextView 中显示如下价格:
价格是双重价值。 a=8.75 和 b=9.00
我需要显示 a=8.75 和 b=9。
我想我需要检查一些东西并改变它的值。但是我不知道是什么。
请帮帮我。谢谢!
double num; //eg 9.00 or 8.75
long beforeDecimal = (long)num;
double afterDecimal = num - beforeDecimal;
if(afterDecimal>0)
display num;
else
display beforeDecimal;
据我从你的问题中了解到,你想在 TextView 中显示以下两个文本:“8.75”和“9”。
如果是这样,请将文本设置为您的 TextView,如下所示:
double priceA = 8.75;
double priceB = 9;
String s = ""+priceA; // or priceB
tv.setText(s);
在 Java 中表示金钱非常棘手。使用 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 一个看起来很有帮助。
我想知道如何在我的 TextView 中显示如下价格:
价格是双重价值。 a=8.75 和 b=9.00
我需要显示 a=8.75 和 b=9。
我想我需要检查一些东西并改变它的值。但是我不知道是什么。
请帮帮我。谢谢!
double num; //eg 9.00 or 8.75
long beforeDecimal = (long)num;
double afterDecimal = num - beforeDecimal;
if(afterDecimal>0)
display num;
else
display beforeDecimal;
据我从你的问题中了解到,你想在 TextView 中显示以下两个文本:“8.75”和“9”。
如果是这样,请将文本设置为您的 TextView,如下所示:
double priceA = 8.75;
double priceB = 9;
String s = ""+priceA; // or priceB
tv.setText(s);
在 Java 中表示金钱非常棘手。使用 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 一个看起来很有帮助。