Stuck in javaHow do I convert my output to doubles for my cu
Stuck in java...How do I convert my output to doubles? for my current output is 2000.0 but I need it to display 2 decimal places, 2000.00.
import java.util.Arrays;
public class TestMerge {
public static void print(CheckBook obj[])
{
int n = obj.length;
for(int i=0;i<n;i++)
System.out.println(obj[i].getPayTo()+\" $\"+obj[i].getAmount()+\" #\"+obj[i].getCheckNumber());
}
public static void main (String [ ] args ) {
CheckBook obj[] = {new CheckBook(\"Bob\", 2000.00, 503),
new CheckBook(\"Newman\", 1150.00, 506),
new CheckBook(\"Carl\", 6527.12, 504),
new CheckBook(\"Andrew\", 1926.00, 500),
new CheckBook(\"Jax\", 3155.00, 501)};
System.out.println ( \" ____________________________\");
print(obj);
System.out.println ( \" ____________________________\");
Arrays.sort(obj);
System.out.println ( \" ____________________________\");
print(obj);
}
}
Solution
public class CheckBook implements Comparable<CheckBook> {
String payTo;
double amount;
int checkNumber;
public CheckBook(String payTo, double amount, int checkNumber) {
super();
this.payTo = payTo;
this.amount = amount;
this.checkNumber = checkNumber;
}
/**
* @return the payTo
*/
public String getPayTo() {
return payTo;
}
/**
* @param payTo
* the payTo to set
*/
public void setPayTo(String payTo) {
this.payTo = payTo;
}
/**
* @return the amount
*/
public double getAmount() {
return amount;
}
/**
* @param amount
* the amount to set
*/
public void setAmount(double amount) {
this.amount = amount;
}
/**
* @return the checkNumber
*/
public int getCheckNumber() {
return checkNumber;
}
/**
* @param checkNumber
* the checkNumber to set
*/
public void setCheckNumber(int checkNumber) {
this.checkNumber = checkNumber;
}
@Override
public int compareTo(CheckBook o) {
// TODO Auto-generated method stub
if (this.getAmount() < o.getAmount())
return 1;
else
return -1;
}
}
import java.text.DecimalFormat;
import java.util.Arrays;
public class TestMerge {
public static void print(CheckBook obj[])
{
int n = obj.length;
DecimalFormat decimalFormat = new DecimalFormat(\"#.00\");
for (int i = 0; i < n; i++)
System.out.println(obj[i].getPayTo() + \" $\"
+ decimalFormat.format(obj[i].getAmount()) + \" #\"
+ obj[i].getCheckNumber());
}
public static void main(String[] args) {
CheckBook obj[] = { new CheckBook(\"Bob\", 2000.00, 503),
new CheckBook(\"Newman\", 1150.00, 506),
new CheckBook(\"Carl\", 6527.12, 504),
new CheckBook(\"Andrew\", 1926.00, 500),
new CheckBook(\"Jax\", 3155.00, 501) };
System.out.println(\" ____________________________\");
print(obj);
System.out.println(\" ____________________________\");
Arrays.sort(obj);
System.out.println(\" ____________________________\");
print(obj);
}
}
OUTPUT:
____________________________
Bob $2000.00 #503
Newman $1150.00 #506
Carl $6527.12 #504
Andrew $1926.00 #500
Jax $3155.00 #501
____________________________
____________________________
Carl $6527.12 #504
Jax $3155.00 #501
Bob $2000.00 #503
Andrew $1926.00 #500
Newman $1150.00 #506


