Sorry for likely a pedestrian ask. What is the recommended way to format a percentage stored in data table service in a data grid as a percent?
2 Likes
hi @stadmanwi and welcome to the forum,
you can use fstrings
mypercentage = 0.123
print(f"{mypercentage:.0%}") #.0 is the number of decimal places
# 12%
Thanks @stucork!
I was hoping for a databinding option. Is there a good example of what to do when you cant use databinding?
sure - in a databinding you can add a python expression.
So assuming your item is something like
self.item = {'mypercentage': 0.31}
and you want to bind the value 0.31
to the text
property of a label component (but formatted)
Then in the design view just add the following to the label’s text databinding
f"{self.item['mypercentage']:.0%}"
I seem to recall that this works for read-only use, but not for writebacks. If the field is being used to edit (change) a percentage, then a different approach will be needed.
1 Like
Ah yes - i was envisaging a label component which wouldn’t have writeback…
…which might well be the case here.