Calculating a percentage value in the database

23 views
Skip to first unread message

mostwanted

unread,
Jan 13, 2020, 11:54:25 AM1/13/20
to web2py-users
I'm trying to calculate a required percentage value for the entered figure but i am getting the wrong answer

Field('unitPrice', 'float', label=SPAN('Unit Price', _style="font-weight: bold;"), requires=IS_MATCH('[0-9]+', error_message=T('Enter Money in Proper Figures'))),
Field('markup', 'integer', label=SPAN('Markup', _style="font-weight: bold;"), requires=IS_NOT_EMPTY()),
Field('markupCalc', 'integer', compute=lambda r: (100.0/float(r['markup'])*float(r['unitPrice']))),
Field('markedupPrice', 'float', compute=lambda r: round(float(r['unitPrice'])+r['markupCalc'])),

On the code above I'm getting the markupCalc field wrong and it affects the markedUpPrice value. How can i correctly calculate a required percentage of a given value?

Mostwanted

mostwanted

unread,
Jan 13, 2020, 12:20:21 PM1/13/20
to web2py-users
I figured out where my problem was, I switched up the arrangement of figures in markedCalc:, I put the dividing value before the markup value as opposed to what i did initially which was bringing it before the markup value and it worked.

Field('markupCalc', 'integer', compute=lambda r: (float(r['markup'])/100*float(r['unitPrice']))),


Ari Lion BR Sp

unread,
Jan 13, 2020, 12:22:16 PM1/13/20
to web2py-users
Field('markupCalc', 'integer', compute=lambda r: (float(r['markup']/100.0)*float(r['unitPrice']))),


??


but why integer?
you will have no precision.....

mostwanted

unread,
Jan 13, 2020, 12:47:56 PM1/13/20
to web2py-users
hahahaha thanks for highlighting that! I initially copied & pasted this line and didnt get rid of some impurities.
Reply all
Reply to author
Forward
0 new messages