1

I need to display currency amount with the symbol and format based on currency code. Currently, I'm using a default locale for each currency code since I don't have access to the exact locale along with the currency code and using NumberFormat.format() to get the formatted currency amount with format and symbol. Does Joda money do this all - provide currency code and it displays the formatted currency with symbol? Any help/direction regarding this is appreciated.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
luv-yzag
  • 35
  • 1
  • 6

2 Answers2

2

I just found out about joda-money, I'm testing it to see if it fits in my project requirements. I read your question and decided to answer it while testing the library.

For what I could see inside the joda-money jar it has very few classes and provide the basic currency management and a formatter.

It seems that at the early stage in which joda-money is the formatter still needs the Locale to print the money symbol, as you can see in my code. (The code is in scala but the methods call are the same in Java)

import org.joda.money.format.MoneyFormatterBuilder
import org.joda.money.{Money, CurrencyUnit}

def formatterBuilder() = new MoneyFormatterBuilder().appendCurrencySymbolLocalized().appendAmount()

def moneyFormatter(locale: java.util.Locale) = formatterBuilder().toFormatter(locale)

def moneyFormatter() = formatterBuilder().toFormatter

val usd: CurrencyUnit = CurrencyUnit.of("USD")
val money = Money.of(usd, 23232312d)                  // or just Money.parse("USD 23232312")

moneyFormatter().print(money)                         // res0: String = USD23,232,312.00
moneyFormatter(java.util.Locale.US).print(money)      // res1: String = $23,232,312.00

As you can see, the Locale is needed to print the '$' symbol.

Additionally I tried with another currency, the yen (Japan currency). I printed it with the US locale and the result was something I didn't spec.

val japan = Money.parse("JPY 23232312")

moneyFormatter().print(japan)                         // res2: String = JPY23,232,312
moneyFormatter(java.util.Locale.JAPAN).print(japan)   // res3: String = ¥23,232,312
moneyFormatter(java.util.Locale.US).print(japan)      // res4: String = JPY23,232,312

EDIT: You could also create an abstract class as a wrapper for Money, like this:

abstract class Currency(amount: BigDecimal, locale: java.util.Locale) {
  val currencyUnit: CurrencyUnit = CurrencyUnit.getInstance(locale)
  val money: Money = Money.of(currencyUnit, amount)
  def formatted: String = new MoneyFormatterBuilder().appendCurrencySymbolLocalized().appendAmount().toFormatter(locale).print(money)
  // implement others Money methods
}

class USDollars(amount: BigDecimal) extends Currency(amount, java.util.Locale.US)
Londo
  • 448
  • 4
  • 9
0
 public static void main(String[] args) throws Exception {

        Currency usd = java.util.Currency.getInstance("USD");
        NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.US);
        format.setCurrency(usd);
        System.out.println(format.format(23232312));
    }

Output

$23,232,312.00
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • This is how I'm doing it now. Any idea if the same can be achieved by Joda money without having to maintain manually the default locale (language, country) for each of the 3 char currency code. – luv-yzag Nov 18 '14 at 08:36