akiong akiong - 1 month ago 9
Groovy Question

multiply between BigDecimal Format ##.00

iam using grails.. i have some problem on bigdecimal format..

for example ..
i have two fields on table "invoice_detail"

price(nullable: false, blank: true, min: new BigDecimal('0.0'), max: new BigDecimal('99999999999999999.99'), scale: 2)

qty(nullable: false, blank: true, min: new BigDecimal('0.0'), max: new BigDecimal('99999999999999999.99'), scale: 2)


and store like this..

id|price | qty |
1 |50000.00|10.00|


when i call it like this..

def invoiceD = InvoiceDetail.get(1)
def total = invoiceD.price * invoiceD.qty


the result will be
500000.0000


if i multiply again with
10.00
it will be
5000000.000000


i want get the result with format
###.00

i have an idea to get a result like that with using
replace
, but i think itsnot a good idea .

Answer

you can use setScale see this BigDecimal setScale and round

def total = invoiceD.price * invoiceD.qty
total = total.setScale(2, BigDecimal.ROUND_HALF_UP)

hope it will help you

Comments