represents currency format

111 views
Skip to first unread message

Omi Chiba

unread,
Jan 26, 2015, 5:39:07 PM1/26/15
to web...@googlegroups.com
I want to show the UnitPrice like $1,000,00 but it shows 1000.00. 

Model

db.define_table('KR_Product',
    Field('ProductCode', length=15),
    Field('PartNumber', length=50),
    Field('SubA', length=15),
    Field('SubB', length=15),
    Field('UnitPrice', 'decimal(18,2)'),
    Field('Quantity', 'decimal(18,0)'),
    Field('Weight', 'decimal(18,3)'),
    Field('LeadTime', length=50),
    Field('Model', length=2),
    Field('Lead', length=2),
    Field('Block', length=1),
    Field('Stroke', length=4),
    Field('Grade', length=1),
    Field('Cover', length=1),
    Field('Sensor', length=1),
    Field('Housing', length=1),
    Field('Flange', length=1),
    Field('Description', length=500)
    )

db.KR_Product.UnitPrice.represent = lambda value, row: '$ %.2f' % (0.0 if value == None else value)


View

<div id="container">            
    <div id="wrapper">
    <table class="table table-hover">
            <thead>
                <th>Part Number</th>
                <th>Lead Time</th>
                <th style="text-align: right;"><span class='right'>Quantity</th>
                <th style="text-align: right;"><span class='right'>Unit Price($)</th>
                <th style="text-align: right;"><span class='right'>Weight(kg)</th>
  </thead>
            <tbody>
                 {{for list in lists:}}
    <tr>
    <td>{{=list.PartNumber}}</td>
        <td>{{=list.LeadTime}}</td>
                <td style="text-align: right;">{{=list.Quantity}}</td>
                <td style="text-align: right;">{{=list.UnitPrice}}</td>
                <td style="text-align: right;">{{=list.Weight}}</td>   
        </tr>
                {{pass}}
            </tbody>
</table>
    </div>
</div>

Niphlod

unread,
Jan 26, 2015, 5:56:59 PM1/26/15
to web...@googlegroups.com
if you're not using web2py's facilities (SQLTABLE, grid, rows.render(), etc), you have to call represent directly

{{=db.KR_Product.UnitPrice.represent(list.UnitPrice)}}

Omi Chiba

unread,
Jan 26, 2015, 6:06:20 PM1/26/15
to web2py-users
Can you give me more details? I'm not sure if directly call it from view or controller. I apprecite if you can provide a sample code with the following.  db.KR_Product.UnitPrice.represent = lambda value, row: '$ %.2f' % (0.0 if value == None else value)

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/bJHpQE5O74g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Jan 26, 2015, 7:01:08 PM1/26/15
to web...@googlegroups.com
in the view, you have

<td style="text-align: right;">{{=list.UnitPrice}}</td>

use instead

<td style="text-align: right;">{{=db.KR_Product.UnitPrice.represent(list.UnitPrice)}}</td>

should work without issues.

or read about render() here

Massimo Di Pierro

unread,
Jan 26, 2015, 9:48:38 PM1/26/15
to web...@googlegroups.com
Notice this can also be done in JS and it will make your code cleaner: http://numeraljs.com/

Omi Chiba

unread,
Jan 27, 2015, 9:44:07 AM1/27/15
to web...@googlegroups.com
I got this error...
<type 'exceptions.TypeError'>(<lambda>() takes exactly 2 arguments (1 given))

Render also looks complicated to me so I will just use the JS Massimo suugested. Thank you both!!

Niphlod

unread,
Jan 27, 2015, 10:31:26 AM1/27/15
to web...@googlegroups.com
whoopsie, since you passed a lambda that takes value, row you should have passed list.UnitPrice, list
Reply all
Reply to author
Forward
0 new messages