[dev] Best strategy to store money values

Jan Schneider jan at horde.org
Wed Aug 17 08:06:16 UTC 2011


Zitat von Luis Felipe Marzagao/Andamentos <lfbm.andamentos at gmail.com>:

> Em 16-08-2011 04:54, Jan Schneider escreveu:
>>
>> Zitat von Luis Felipe Marzagao <lfbm.andamentos at gmail.com>:
>>
>>> Hello:
>>>
>>> I´m using Horde_Rdo, which, by the way, is fantastic, in order to
>>> develop an app to control clients' credits and debits.
>>>
>>> My first impulse was to store the money amount of each transaction as
>>> decimal(10,2) (the backend I´m testing is mysql, but I´d like to keep
>>> things as abstracted as possible, taking advantage of Horde_Rdo).
>>>
>>> But then I got confused about what type of Horde_Form variable and
>>> column type (in migration style terms) should I use.
>>>
>>> I have never developed any app involving money values and
>>> calculations, so I could use some advice concerning the best
>>> strategy/practice before I invest time going the wrong direction.
>>>
>>> Any pointers are appreciated.
>>
>> The form type should be "number", the column type "float".
>>
>> Jan.
>>
> Thanks. I still have to do some manually conversion, because the  
> "number" form type only validates and returns the number wih your  
> local decimal point (in my case the comma ',', not the point '.'),  
> which is not accepted by mysql float type. It seems mysql expects to  
> receive a pointed decimal number (like 500.10), otherwise it will  
> loose the digits after the decimal point. But this is something  
> manageable.

Just make sure that the value passed to Horde_Db is really a float.  
And you need an up-to-date Horde_Db version, because older versions  
had a bug with float values.

> Let me enjoy the opportunity to ask how do I tell migrations create  
> the column type 'float' with the options (12,2), twelve digits, two  
> being after the decimal point. I can't find any reference on how to  
> pass the correct parameters to $t->column( .... array(????));

Hm, the code is a bit confusing, maybe you need to use 'decimal'  
instead of 'float'. In any case, it takes 'precision' and 'scale'  
options.

Jan.

-- 
Do you need professional PHP or Horde consulting?
http://horde.org/consulting/



More information about the dev mailing list