Floating Point Issue, how to resolve the same?

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.



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-forum+un...@googlegroups.com.

To post to this group, send email to er...@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.

 

 

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

On 27-Jun-2013, at 6:52 AM, Addy <ad...@rigpl.com> wrote:

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.



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-forum+un…@googlegroups.com.

To post to this group, send email to er…@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.

 

 




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-forum+un...@googlegroups.com.

To post to this group, send email to er...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/0B7D78E1-0AE5-4534-BCC9-FB925D546C80%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 

Hi Aditya,

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

-Anand.

On 27-Jun-2013, at 9:51 AM, Rushabh Mehta <rm...@gmail.com> wrote:

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

On 27-Jun-2013, at 6:52 AM, Addy <ad...@rigpl.com> wrote:

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.



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-forum+un…@googlegroups.com.

To post to this group, send email to er…@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.

 

 





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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/0B7D78E1-0AE5-4534-BCC9-FB925D546C80%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 




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-forum+un...@googlegroups.com.

To post to this group, send email to er...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/D7876E3C-E314-4399-B2D3-B063B3D567D1%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 

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.


On Thu, Jun 27, 2013 at 11:07 AM, Anand Doshi <an...@iwebnotes.com> wrote:
Hi Aditya,

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


-Anand.

On 27-Jun-2013, at 9:51 AM, Rushabh Mehta <rm…@gmail.com> wrote:

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





On 27-Jun-2013, at 6:52 AM, Addy <ad...@rigpl.com> wrote:

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.



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-forum+un…@googlegroups.com.

To post to this group, send email to er…@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.








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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/0B7D78E1-0AE5-4534-BCC9-FB925D546C80%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.







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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/D7876E3C-E314-4399-B2D3-B063B3D567D1%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.





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-forum+un...@googlegroups.com.

To post to this group, send email to er...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/CAEmKBHwYzcytR02%3Dw-_ru8fZ2%3DiOdyQFJnY9nE-wr2ve6o8dTQ%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 

Hi Aditya,

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

Thanks,
Anand.

On 27-Jun-2013, at 11:40 AM, Aditya Duggal <ad...@rigpl.com> wrote:

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.


On Thu, Jun 27, 2013 at 11:07 AM, Anand Doshi <an...@iwebnotes.com> wrote:
Hi Aditya,

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


-Anand.

On 27-Jun-2013, at 9:51 AM, Rushabh Mehta <rm…@gmail.com> wrote:

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





On 27-Jun-2013, at 6:52 AM, Addy <ad...@rigpl.com> wrote:

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.



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-forum+un…@googlegroups.com.

To post to this group, send email to er…@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.

 

 





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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/0B7D78E1-0AE5-4534-BCC9-FB925D546C80%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 





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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/D7876E3C-E314-4399-B2D3-B063B3D567D1%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 




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-forum+un…@googlegroups.com.

To post to this group, send email to er…@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/CAEmKBHwYzcytR02%3Dw-_ru8fZ2%3DiOdyQFJnY9nE-wr2ve6o8dTQ%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.

 

 




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-forum+un...@googlegroups.com.

To post to this group, send email to er...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/60052A8D-AC8F-427D-BD1E-15F690386939%40erpnext.com.

For more options, visit https://groups.google.com/groups/opt_out.