Reputation: 77
Input: float or double amount
Output: Number or String in US currency format
Am currently using a DecimalFormater for converting float or double. The output of it is the amount in string format.
For formatting in US Currency format Am using NumberFormat.getInstance(Locale.US).format(--Input Value--). But input value that accepted is only Numeric data style, not string data type.
So the input from DecimalFormater cannot be used as input to NumberFormat
Tried Using Wrapper class to convert the string to specific numeric type. But the round off to two decimal not working as excepted.
Method that Convert double value into String and double to US Format
public static string doubleInTwoDecimalPoint(double value) {
DecimalFormat df = new DecimalFormat("####0.00");
String doubleInStringRep = df.format(value);
return doubleInStringRep;
}
public static void convertToUSFormat(double amount) {
System.out.println(NumberFormat.getInstance(Locale.US).format(amount));
}
Upvotes: 0
Views: 2350
Reputation: 4266
You need to use NumberFormat::getCurrencyInstance which will format to the Locale
you pass to it.
System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(amount));
Example:
Input:
123.4567
Output:
$123.46
Upvotes: 2