I have records with an amount field which is summed using Sum and Groupby 
and the resulting rows displayed via SQLTABLE. I want to format the summed 
amounts with 2 decimal places, right justified and with thousands 
separators. The decimal places and right justification are working fine 
but, despite lots of experimentation, I just can't see how to get the 
thousands separators.

An example of the current page can be seen at 
www.appgov.org/apg/la/laspsums/386

The controller code for this page is:  

    query = (db.laspend.laname == session.laname)                    # get 
the spend records for this LAname
    isf = db.laspend.sourcefile
    isum = db.laspend.amount.sum()
    icount = db.laspend.amount.count()
    rows = db(query).select(
           isf, isum, icount,
           orderby=db.laspend.sourcefile,
           groupby=db.laspend.sourcefile)                    
    for row in rows:
        if row._extra[isum] is not None:                                   
        # to avoid Type error on int + NoneType
            session.totamtsp = session.totamtsp + row._extra[isum]
        if row._extra[icount] is not None:                                 
        
            session.totcountsp = session.totcountsp + row._extra[icount]
        session.totfile += 1
    hdrs = {'laspend.sourcefile':T('Source file'),
            'SUM(laspend.amount)':T('Overall amount'),
            'COUNT(laspend.amount)':T('Records')}
    form = SQLTABLE(rows,headers=hdrs, truncate=40, _id='laspsum')    # _id 
is the CSS class to get right align for numeric fields.

the Table definition is:

# LA Spend table
db.define_table('laspend',
    Field('laname', type='string',
          label=T('LA Name')),
    Field('invdate', type='date',
          label=T('Inv Date')),
    Field('amount', type='decimal(12,2)',
          label=T('Amount')), 

and the View is:

<h4>Summary of currently loaded LA spend data .....</h4>
<p>{{=form}}</p>
<p><b>Total amount : </b>£{{='{0:,}'.format(session.totamtsp)}} in 
{{='{0:,}'.format(session.totcountsp)}} records from {{=session.totfile}} 
source files</p>

I can get thousands separators for rows with *individual* amounts using 
represent  eg 
    db.laspend.amount.represent  = lambda value, row: DIV('{0:,}'.format(0 
if value == None else value), _style='text-align: right;')   # with 
thousands seps
but I can't see how to do it for the summed amounts.

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to