Forum OpenACS Q&A: Re: ad_form and currency widget

Collapse
Posted by Jack Ramsey on
This is only a couple of years late, but I was running into the same problem and figured out an answer.  Maybe someone else working with ad_form and currency will benefit from this answer.

In the database I created the following field:

price numeric(12,2)

In the -form part of ad_form I used the following:

{price:currency,optional,to_sql(sql_number)
{label "Price"}
{value {$price}}}

This should be fine to insert or update the price into the DB without conversion:

update db
set price = :price
where something = :this

When selecting the value to be displayed in the form you have to do some to_char trickery:

select
to_char(price, 'L 999999999999 . 99') as price
from db
where something = :this

Hope this saves someone some time.  It would have saved me a couple of hours.

Collapse
Posted by Mark Aufflick on
And a couple of years later...

I always had a problem with this where 0 values would end up with 00 in the cents box and a decimal point in the dollar box, because the output of this postgres to_char format is:

.00

which confuses a regexp somewhere.

All you need to do is tweak the format:

to_char(price, 'L 999999999990 . 99') as price

Note the last value is a 0 - that means that char should be 0 padded (ie. use a 0 instead of blank) which 9 defaults to only after the decimal place.