PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to fix incorrect decimal places of a multiplication result

 August 11, 2022     decimal, floating-point, python, string     No comments   

Issue

I'm having a problem with the following calculation:

>>> print(float('32.31') * 1e9)
32310000000.000004

I need this result to be 32310000000.0 (without the false decimal place). This also occurs when using a float directly:

>>> print(32.31 * 1e9)
32310000000.000004

Is there a way to avoid the false decimal place? Analysing the string and rounding to the number of decimal places is not a pefered solution.

Many thanks in advance.

NOTE: The following works fine:

>>> print(32.32 * 1e9)
32320000000.0

so I'm really happy I found the problem above during testing.

EDIT: Thank you for your quick answers! Sorry, I've missed an important point. The method must also work for when the result is less than one, e.g.:

32.31 * 1e-9

...in this case I cannot use round(32.31 * 1e-9, 1)


Solution

One way to avoid your problem is to use the decimal module, which works in base ten and thus works the way humans would work (if we were much faster).

from decimal import Decimal
value = float(Decimal('32.31') * Decimal(1e9))

This yields the value you want,

32310000000.0

Another way is to use the fractions module, which works with exact values:

from fractions import Fraction
value = float(Fraction('32.31') * Fraction(1e9))

Note that in both of these methods, we must convert 1e9 and not just the decimal value. Using 1e9 as a float converts the intermediate values to float and the approximation problem pops up again. In either method, you could leave off the final conversion to float type and just continue to work with a Decimal or Fraction value. Either of these methods is somewhat slower than using float types: you gain accuracy at the expense of speed. That speed decrease may matter in some situations.


Regarding your edit to your question: using the value 1e-9 in either of my methods will still result in what you want, namely the value 3.231e-08.



Answered By - Rory Daulton
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing