LoginSignup
4
4

More than 5 years have passed since last update.

通貨の表記で複数の条件を設定したいときの書き方

Last updated at Posted at 2014-08-13

javaで通貨の表記を変更するには、DecimalFormatを使うことが多い。
例えばUSドルの表記にしたい場合($1,000など)、以下のように書く。

private static NumberFormat COMMA_FORMAT = new DecimalFormat("$ #,###");
String formatDollar = COMMA_FORMAT.format(1000);  // -> $1,000

USでは、マイナスの場合は( \$ 1,000 )のように書くのが一般的だが、上記のままだと - \$ 1,000のようになってしまう。

これを( \$1,000 )と書くには以下のようにセミコロンで表示したい表記で書けばOK。

    private static NumberFormat COMMA_FORMAT = new DecimalFormat("$ #,###;($ #,###)");
String formatDollar = COMMA_FORMAT.format(-1000);  // -> ($1,000)

参考

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4