Carefully read and answer the questions on this test. You may assume the stateme
ID: 3686485 • Letter: C
Question
Carefully read and answer the questions on this test. You may assume the statement import java. Util. scanner; for all question on this test. Write a Java definition for a method that converts a double value to a currency-formatted/astir format As Money (double) String method converts a double value to a String with a dollar sing and two decimal digits ex: formatAsMoeny (1234. 56789) "dollar 12346.56" ex: formatAsMoeny (0) "dollar 0.00" ex: formatAsMoeny(-9876.54321) "-dollar 9876.54" ex: formatAsMoeny (500) "dollar 500.00" ex: formatAsMoeny(- 500) "dollar 500.00" astir/Explanation / Answer
Solution:
public class DoubleToCurrency {
public static void main(String[] args) {
//Variable declaration goes here
double money = 3.98944;
//Calling method formatAsMoney and passing variable 'money' as an parameter.
String outputStr = formatAsMoney(money);
System.out.println( "The formated string " + outputStr);
}
private static String formatAsMoney(double money) {
String str = "$" + String.format( "%.2f", money);
return str;
}
}
Sample run :
The formated string $3.99