I need to create a method that returns the total amount of money in dollar notation with two decimal places. Any help would be wonderful. This is what I have so far...
private final double QVALUE = 0.25;
private final double DVALUE = 0.10;
private final double NVALUE = 0.05;
private final double PVALUE = 0.01;
public double dollarValue() {
double amount = (quarters * QVALUE) + (dimes * DVALUE) +
(nickels * NVALUE) + (pennies * PVALUE);
return amount;
}
I have provided the values for each coin and the method I need to return a double in. Thanks!
Ex. When I get $1.50 for the total amount of money, it displays as $1.5, I need it to display $1.5 and I do not want to use DecimalFormat because it needs to return a String
I don't understand what you mean by "it needs to return a String". Can't you just use String.format("%.2f", d);
?
May be this is what you want:
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
String s = currencyFormat.format(1.5);
Result is "$1.50"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.