Please help. I need to write a class called DblOutput that contains methods that
ID: 3631461 • Letter: P
Question
Please help. I need to write a class called DblOutput that contains methods that take in a double and output it in various formats. The methods should include:public static void writeDollar (double d) // writes out d as a US currency amount $x.xx
public static void writeFraction(double d) // writes d as a fraction
public static void writeDecFormat (double d, int places) // writes d as a decimal with specified number of decimal places
public static void writeSciFormat (double d, int places) // writes d in scientific format (using e notation)
Additional methods not listed above will be appreciated.
The code should be written using a rudimentary skill set. To date, the most complicated topic we have covered is nested loops.
Thanks you!!!
Explanation / Answer
public static void writeDollar (double d)
{
NumberFormat nf = NumberFormat.getInstance(Locale.US);
System.out.println("Dollar format:$"+nf.format(d));
}
public static void writeDecFormat (double d, int places)
{
String str="0.";
for(int i=0;i<places;i++)
str=str+"#";
System.out.println(str);
NumberFormat formatter = new DecimalFormat();
formatter = new DecimalFormat(str);
System.out.println(formatter.format(d));
}
public static void writeSciFormat (double d, int places)
{
String str="0.";
for(int i=0;i<places-2;i++)
str=str+"#";
str=str+"E0";
NumberFormat formatter = new DecimalFormat();
formatter = new DecimalFormat(str);
System.out.println(formatter.format(d)); // 1.2345E-1
}