float usage; 2 decimals

369 views
Skip to first unread message

Stefan van den Eertwegh

unread,
Jul 11, 2014, 5:50:30 AM7/11/14
to web...@googlegroups.com
Hi,

I have a float type in the define tables and when he inserts 3.50 into the database he makes 4.0 off it.
How comes that it rounds off the float? And not uses the usage of 2 decimals?

Thank you!

Stefan van den Eertwegh

unread,
Aug 14, 2014, 2:20:53 PM8/14/14
to web...@googlegroups.com
Is there anyone who knows why this is happening?

Please, anyone?

Op vrijdag 11 juli 2014 11:50:30 UTC+2 schreef Stefan van den Eertwegh:

Oli

unread,
Aug 14, 2014, 3:03:45 PM8/14/14
to web...@googlegroups.com
please show your model, comtroller and view

Cliff Kachinske

unread,
Aug 14, 2014, 3:39:56 PM8/14/14
to
What database? 

Web2py has a decimal datatype which, in my use cases, mimics the Python decimal datatype. I use Postgresql and the adapter turns Python decimal into Postgres numeric. Both of these types act like real world decimal numbers. In other words, .2 + .1 comes out to .3. Of course a different db backend may work differently.

Just google python decimal.

Stefan van den Eertwegh

unread,
Aug 15, 2014, 4:22:31 AM8/15/14
to web...@googlegroups.com
I use in the model the type float and MySQL as backend database and MySQL uses INT() as datatype for that field....

So i must use decimal(10,2) as field type in the model and as backand db postgres?

Op donderdag 14 augustus 2014 21:39:56 UTC+2 schreef Cliff Kachinske:
What database? 

Web2py has a decimal datatype which, in my use cases, mimics the Python decimal datatype. I use Postgresql and the adapter turns Python decimal into Postgres numeric. Both of these types act like real world decimal numbers. In other words, .2 + .1 comes out to .3. Of course a different db backend may work differently.

Just google python decimal.

On Friday, July 11, 2014 5:50:30 AM UTC-4, Stefan van den Eertwegh wrote:

Marin Pranjić

unread,
Aug 15, 2014, 4:26:31 AM8/15/14
to web2py-users
Can you show us your table definition?
Web2py doesn't have float. There are double and decimal field types.
You don't need to use postgres.

Marin


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan van den Eertwegh

unread,
Aug 15, 2014, 4:31:26 AM8/15/14
to web...@googlegroups.com
db.define_table(
    'invoice_rule',
    Field('invoice', db.invoice, label=T('Invoice no.')),
    Field('amount', 'float', label=T('Amount')),
    Field('product', 'float', label=T('Product'), requires=IS_EMPTY_OR(IS_IN_DB(db, db.product.id, db.product._format))),
    Field('description', 'string', label=T('Description')),
    Field('date', 'date', label=T('Date')),
    Field('vat_percentage', 'string', requires=IS_IN_SET(settings.vat_percentage), label=T('VAT')),
    Field('quantity', 'float', label=T('Quantity'))
)


Met vriendelijke groet,
Stefan van den Eertwegh


You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/IUs0Zjgmseo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Marin Pranjić

unread,
Aug 15, 2014, 4:35:49 AM8/15/14
to web2py-users
Can you change "float" to "double" and try if it fixed the issue?

Marin

Stefan van den Eertwegh

unread,
Aug 15, 2014, 4:42:30 AM8/15/14
to web...@googlegroups.com
Yes, that fixed the issue. but only for a new field, it adds the new field in mysql as type double.
If i change a current field in the model to double than it stays as int in mysql.

Shall i change the fields manually in mysql?

Op vrijdag 15 augustus 2014 10:35:49 UTC+2 schreef Marin Pranjić:

Marin Pranjić

unread,
Aug 15, 2014, 5:00:57 AM8/15/14
to web2py-users
Do you need to keep existing data?

If you don't, the easiest solution would be to drop affected tables, remove their *.table files (stored in databases directory), and let web2py recreate it.

If you need to keep the data, but you don't care about those float/double fields, you can manually change those fields.

[I wonder why web2py didn't automigrate...]

Stefan van den Eertwegh

unread,
Aug 15, 2014, 5:53:58 AM8/15/14
to web...@googlegroups.com
Thank you for you're information.
I have manually changed the fields to keep the data.

Op vrijdag 15 augustus 2014 11:00:57 UTC+2 schreef Marin Pranjić:
Reply all
Reply to author
Forward
0 new messages