Form input show decimal fields with 4 decimals? (How to format input field in forms?)

406 views
Skip to first unread message

Tom Øyvind Hogstad

unread,
Nov 29, 2014, 11:58:56 AM11/29/14
to web...@googlegroups.com
I have the following field declaration

Field('test_BOF', 'decimal(10,2)',
    label='BOF (mg/l)',
    requires=IS_EMPTY_OR(IS_DECIMAL_IN_RANGE(dot=','))

In a SQLFORM this vil output as a number with a for numbered fraction e.g. 12,3000

<input class="decimal" id="plant_service_test_BOF" name="test_BOF" type="text" value="12,3000">

How can i format this value?

Web2Py: 2.9.11
DB: PostgreSQL 9.3

Tom Ø.

(BTW maybe w2p should detect the requested number of decimals from the field type in this case?)

Richard Vézina

unread,
Dec 2, 2014, 4:18:37 PM12/2/14
to web2py-users
Hello Tom,

The validator don't determine the number of places you want base on your decimal field definition, you have to specified it in the validator : IS_DECIMAL_IN_RANGE(0.0000, 99999999.9999, dot=',')

Notice you have only 2 places in your decimal definition field model!!

Richard

--
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.

pbreit

unread,
Feb 5, 2015, 12:19:08 AM2/5/15
to web...@googlegroups.com
I think I'm having the same problem. All of my prices are decimal(9,2) but they are printing 4 decimal places:

On my Mac with SQLite, they are printing OK with 2 places.

On production, I'm using Postgres but a checked the DB and it's only storing two places.

I didn't see anything in my validators or elsewhere.

pbreit

unread,
Feb 5, 2015, 12:36:32 AM2/5/15
to web...@googlegroups.com
And when number is ##.00 it's dropping off the decimals entirely:

Richard Vézina

unread,
Feb 5, 2015, 10:33:08 AM2/5/15
to web2py-users
I use represent to prevent this kind of stuff...

represent=lambda value, row: '{0:.1f}'.format(value) if value is not None else T('N/A')),

With microformat language provide by .format()

It allows me to also, extend my field domain (more places) and keep the display unchanged at webapp level...

Richard

Reply all
Reply to author
Forward
0 new messages