Best practice to represent Money (value + currency

2020-06-13 08:57发布

I'm not much familiar to Java Currency type, and how it being used in Grails. Though, I'm yet to use it, I saw a tag <g:currencySelect> to use in the views. So, how do I represent it in the domain class.

class Money {
BigDecimal value
Currency currency
....
}

or is there a better sol, which compares diff money objects, format according to the locale ( ',' in EU for separator etc)

thanks in advance. Babu.

5条回答
够拽才男人
2楼-- · 2020-06-13 09:00

since the original answer seems to be outdated, you might want to take a look at the money-plugin which is fresh and currently maintained.

查看更多
该账号已被封号
3楼-- · 2020-06-13 09:07

You might want to take a look a the Currencies plugin. It provides a Money class for holding monetary amounts of differing currencies. They can be embedded into domain classes like so:

class CustomerTransaction {
    Date date = new Date()
    Money amount

    static embedded = ['money']
}
查看更多
4楼-- · 2020-06-13 09:16

Use the JScience library. It's just a shame it's not in a Maven repo yet, and doesn't have a Groovy wrapper to make it Groovier (TM).

查看更多
做自己的国王
5楼-- · 2020-06-13 09:16

If you want to have also live exchage rates updates then following plugin could help https://grails.org/ExchangeRates+Plugin all rates are pulled from Yahoo Finance.

查看更多
倾城 Initia
6楼-- · 2020-06-13 09:18

You should use BigDecimal. Groovy and Grails has excellent native support for it as a datatype on GORM domain classes as well. For reasoning behind using it, see here

What is the best data type to use for money in java app?

查看更多
登录 后发表回答