If a number (float) is input such as 12500, the number is converted to money format as 12,500.00. The format will be '###,###,###.##'. How to do this using java and without any apache utils or similar libraries. A comma will repeat after each 3 digits, and the decimal number will be rounded and shown as 2 digits after decimal places. If .5 it should be .50 and if .569 it should be .57. Is there any solution to this problem through regular expression or anything?
3514 次
2 に答える
4
Googleで少し検索すると、次のコードが見つかりました...参考になるかもしれません...
このリンクを確認してください。理解するのに非常に役立ちます。このリンクでは、必要な形式への道を示しているだけです...
http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html
static public void displayCurrency( Locale currentLocale) {
Double currencyAmount = new Double(9876543.21);
Currency currentCurrency = Currency.getInstance(currentLocale);
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance(currentLocale);
System.out.println(
currentLocale.getDisplayName() + ", " +
currentCurrency.getDisplayName() + ": " +
currencyFormatter.format(currencyAmount));
}
上記のコード行によって生成される出力は次のとおりです。
French (France), Euro: 9 876 543,21 €
German (Germany), Euro: 9.876.543,21 €
English (United States), US Dollar: $9,876,543.21
この場合、ロケールを選択して、特定の国/場所で使用されている形式を知らなくても楽しむことができます。
于 2012-11-08T06:19:21.287 に答える
2
public static void main(String args[])
{
float amount = 2192.05f;
NumberFormat formatter = new DecimalFormat("###,###,###.##");
System.out.println("The Decimal Value is:"+formatter.format(amount));
}
于 2012-11-08T06:14:23.293 に答える