Java method Write a method called currencyFormat that accepts a double as a para
ID: 3667292 • Letter: J
Question
Java method
Write a method called currencyFormat that accepts a double as a parameter and returns that number in USD currency format as a String. For example, the parameter 123456.789 as a double would be returned as $123,456.79 and the parameter 0 would be returned as $0.00 indicating that we always have two digits to the right of the decimal point, also note that appropriate rounding is done for currency format. Another example is shown below, for negative numbers. You may assume that the passed parameter is in the range [999999, -999999] so only one comma and one decimal will be needed.
you cannot just import NumberFormat, nor import anything else here. Use String, concatenation, for loop, if...
Explanation / Answer
import java.math.*;
import java.util.Scanner;
public class Currency {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the decimal Number : ");
String decNum = scanner.next();
BigDecimal payment = new BigDecimal(decNum);
System.out.println(String.format("$%.2f", payment));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.