Friday, February 20, 2009

Xpages, formmating currency client side for multiplication

I needed to multiply a qty times a value and get a total on an xpage, sounds easy until you start getting nan, so i learned a little dojo to convert to and from currency.

On the onblur event of the field i put this on the client side of both the qty field and amount field.

//get the string values
qty1 = XSP.getElementById('#{id:qty1}').value
amount1 = XSP.getElementById('#{id:amount1}').value

//get the dojo currency code
dojo.require("dojo.currency");

//get the numeric value using parse
amount2 = dojo.currency.parse(amount1, {currency: "USD"})

//multiply
total1 = qty2 * amount2

//set total value as currency using format
XSP.getElementById('#{id:total1}').value = dojo.currency.format(total1, {currency: "USD"})

//make sure the amount is in currency format using format
XSP.getElementById('#{id:amount1}').value = dojo.currency.format(amount2, {currency: "USD"})

3 comments:

John Smart said...

Mark,

What timing! This is 90% of what I'm needing. Do you how to get the most-local version of these fields so this technique can be used within a repeat control?

Mark Hughes said...

I am afraid i do not, there are probably some that do though, you might ask Paul Hannan over at IBM, he is very good about getting back to people with real answers.

Mark Hughes said...

By the Way you can do it in server side js as well, look at getItemValueCurrency()

Post a Comment