Floating Point Issue, how to resolve the same?

14 views
Skip to first unread message

Addy

unread,
Jun 26, 2013, 9:22:21 PM6/26/13
to erpnext-dev...@googlegroups.com
Hi,

We have a custom code running for creating description automatically, it works fine but lately we have seen a lot of examples where the description generated is rounded off.

The code being used is 

t = '{0:.3g}'.format(s)

Now the problem is that when a person enters 12.99 in place of s the system is returning t as 13 instead of 12.99

I am unable to find out why is it giving me such output.

Rushabh Mehta

unread,
Jun 27, 2013, 12:21:13 AM6/27/13
to erpnext-dev...@googlegroups.com
Aditya,

Can you replicate this? In what report, with what numbers?

FYI - I get the following input in Python shell

>>> '{0:.3g}'.format(12.99)
'13'
>>> '{0:.4g}'.format(12.99)
'12.99'

best,
Rushabh



T: @rushabh_mehta

--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/a073b716-2a40-46db-888c-7fd25553ba78%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anand Doshi

unread,
Jun 27, 2013, 1:37:21 AM6/27/13
to erpnext-dev...@googlegroups.com
Hi Aditya,

Use:
'{0:.3f}'.format(12.99)

-Anand.

Aditya Duggal

unread,
Jun 27, 2013, 2:10:46 AM6/27/13
to erpnext-dev...@googlegroups.com
Hi Anand,

Using '{0:.3f}'.format(s) would definitely resolve the issue in case s= 12.99 but what if s=15 then it would give an output of 15.000

Would using  '{0:.4g}' instead of .3f not result in any heart burns later. Since I am unable to foresee a problem with .4g as I don't want the trailing zeros.


Anand Doshi

unread,
Jun 27, 2013, 2:19:15 AM6/27/13
to erpnext-dev...@googlegroups.com
Hi Aditya,

Googled about this and found this solution:
"%s" % (n if float(n) == int(n) else round(n, 3),)

Thanks,
Anand.

Reply all
Reply to author
Forward
0 new messages