xmlqwebviewqweb

How to print only first two digits of the floating value (round down) in QWeb


Hi I need to print the value 10.50785 as 10.50 instead of 10.51.

I tried like this <t t-esc="'{0:,.2f}'.format(float(10.50785))"/>, but it's returning the value as 10.51.


Solution

  • In your _wrapped_report_class, define a function like

    _floor(self, value):
        return math.floor(value * 100) / 100 # Since you'd like to get 2 digits it's 100
    

    or if you may want to use the same thing with a different number of digits:

    _floor(self, value, n):
        return math.floor(value * math.pow(10, n)) / math.pow(10, n)
    

    Don't forget to import the math lib on the top of the class.

    import math
    

    Then in your qweb t-esc:

    <t t-esc="'{0:,.2f}'.format(floor(10.50785))"/>
    

    This is just an example of calling defined function in the wrapped report class from the qweb, but I hope you got the idea.