[pygtk] floating point precision
Tim Evans
t.evans at aranz.com
Tue Mar 8 03:39:10 WST 2011
On 2011-03-08 07:30, Timo wrote:
> On 26-02-11 21:13, Johann Schatzer wrote:
>> Using TreeStore with "float" column, e.g.
>> self.treestore = gtk.TreeStore(float)
>>
>> the widget shows floating point numbers with a precision of 6 digits,
>> like 34.234566 or 22.000000
>>
>> How can you get a precision of only 1 or 2 digits, like 34.23 or 22.0 ?
>>
> I would like to know the same. Here is a simple testcase which
> demonstrates the problem.
>
> Cheers,
> Timo
>
> import gtk
>
> MYFLOAT = 12.3456
>
> def main():
> win = gtk.Window()
> win.resize(300, 200)
> win.connect('delete-event', gtk.main_quit)
>
> liststore = gtk.ListStore(float, float)
> treeview = gtk.TreeView(liststore)
> renderer = gtk.CellRendererText()
> col = gtk.TreeViewColumn("Actual", renderer, text=0)
> treeview.append_column(col)
> col = gtk.TreeViewColumn("Rounded", renderer, text=1)
> treeview.append_column(col)
>
> liststore.append([MYFLOAT, round(MYFLOAT, 2)])
>
> win.add(treeview)
> win.show_all()
> gtk.main()
Make the second column a string, and add the formatted value to it. If
you want two decimal places, use '%.2f' % MYFLOAT.
--
Tim Evans
More information about the pygtk
mailing list