Thanks Massimo and everybody else all the alternatives. I've been
carefully thinking on it and IMO storing in the database as "float" is
the best alternative:

PROS
- No conversion is needed to display the amount (just formatting)
- You can't make the sql engine to compute a sum() or any oher
aggregate operation as a query.
- You know that what is in your db is ALMOST what you meant.

CONS
- You must take care of formatting
- If you make use of very big and very small numbers and have A LOT of
records. This is not very likely  to happen in most cases.

I'll do it that way.


On 22 mar, 23:03, mdipierro <[email protected]> wrote:
> Another possibility:
> Make the field 'string' and try this
>
> class IS_DECIMAL:
>    def __init__(self,decimals=2,error_message="Invalid"):
>         self.decimals=decimals
>         self.error_message=error_message
>    def __call__(self,value):
>         import re
>         if re.compile('\d+(\.\d{1,%s})?'%self.decimals).match(value):
>              return (value,None)
>         else:
>              return (value,self.error_message)
>
> On Mar 22, 12:19 pm, Paco <[email protected]> wrote:
>
> > All right, I knew about the implieddecimalpoint solution but it won
> > allow you to handle very big numbers.
>
> > I made a quick check and only SQLite doesn't supportDECIMALtypes.
>
> > Maybe is worth the effort to make DAL support, for the sake of
> > simplicity, a MONEY type and make DAL to 'internaly' supportdecimal
> > types in SQLite with implieddecimalpoint.
>
> > On 21 mar, 03:45, mdipierro <[email protected]> wrote:
>
> > > try this:
>
> > > class IS_DECIMAL:
> > >    def __init__(self,decimals=2,error_message="Invalid"):
> > >         self.decimals=decimals
> > >         self.error_message=error_message
> > >    def __call__(self,value):
> > >         try:
> > >              value=int(float(value)*(10**self.decimals))
> > >              return (value,None)
> > >         except:
> > >              return (value,self.error_message)
> > >     def format(self,value):
> > >         return float(value)/10**self.decimals
>
> > > On Mar 20, 7:41 pm, Joe  Barnhart <[email protected]> wrote:
>
> > > > The easiest workaround is to use INTEGER and leave all amounts in
> > > > cents, i.e. 123 instead of 1.23.  If all math is done with the implied
> > > >decimalpoint on integers, you will never have floating-point round-
> > > > off errors.  Only put in thedecimalpoint when you are ready to
> > > > display the value.  (Massimo -- Maybe a validator can help here?)
>
> > > > -- Joe B.
>
> > > > On Mar 20, 12:03 pm, Paco <[email protected]> wrote:
>
> > > > > I've been trying web2py for a couple of days and I believe is just
> > > > > great. I'm so excited that I'm planning to build my own accounting and
> > > > > invoicing application, which is, in the other hand, not so complex in
> > > > > my case.
>
> > > > > I need (and I believe many more people do) aDECIMALfield type or
> > > > > maybe a CURRENCY type since there are many applications that need
> > > > > fixed precision numbers.
>
> > > > > It seems to me that is shouldn't be very difficult to do since most
> > > > > databases, including SQLite, support NUMERIC orDECIMALtypes.
>
> > > > > It would be a great newfeature. Are there any plans to do such a
> > > > > thing? Or maybe there is a nice workaround to do this in web2py?
>
> > > > > Cheers,
> > > > > Paco
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to