Question:
I have this table in web2py:
db.define_table('gastos_generales',
Field('numero_ticket_gasto', 'string'),
Field('tipo_centro_costo', 'reference tipos_centros_costos',requires=IS_EMPTY_OR(IS_IN_DB(db, db.tipos_centros_costos, '%(nombre)s', zero='---- Elegir Opcion ----'))),
Field('numero_centro_costo_proyecto', 'string'),
Field('fecha_ticket_gasto', 'date'),
Field('tipo_pago', 'reference tipos_pagos',requires=IS_EMPTY_OR(IS_IN_DB(db, db.tipos_pagos, '%(nombre)s', zero='---- Elegir Opcion ----'))),
Field('moneda', 'reference monedas',requires=IS_EMPTY_OR(IS_IN_DB(db, db.monedas, '%(nombre)s', zero='---- Elegir Opcion ----'))),
Field('detalle_ticket_gasto', 'string'),
Field('detraccion', 'boolean'),
Field('porcentaje_detraccion', 'double'),
Field('total_venta', 'double'),
Field('monto_detraccion', compute=lambda r: round(r['total_venta'] * r['porcentaje_detraccion'])),
Field('fecha_recepcion', 'date'),
Field('monto_sub_total', 'float'),
Field('monto_impuesto', 'float'),
Field('monto_total_gasto', 'float'),
Field('nota', 'string'),
format='%(numero_ticket_gasto)s')
The monto_detraccion
field is the result of total_venta
by porcentaje_detraccion
, but I don't want the value to be rounded up, but to two decimal digits.
Answer:
If you don't pass the second parameter (the number of digits) to the round
function, then it won't show you decimals.
If you need, for example, two decimal places you can modify the field like this:
db.define_table('gastos_generales',
# ...
Field('monto_detraccion', compute=lambda r: round(r['total_venta'] * r['porcentaje_detraccion'], 2)),
# ...
)
Although if you are working with decimals, you should ideally work with the decimal
module, in web2py you would have to change the fields from double
to decimal(n, m)
.
Look at Field types .