multiplying integer to decimal - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: multiplying integer to decimal (/thread-3713.html) |
multiplying integer to decimal - ArnabRoyBatman - Jun-16-2017 I was trying a multiplication in python c = 0.72*5 instead of giving me 3.6 it returned 3.599999997 something like that... whats that about ???? !!! RE: multiplying integer to decimal - buran - Jun-16-2017 https://en.wikipedia.org/wiki/Floating-point_arithmetic RE: multiplying integer to decimal - snippsat - Jun-16-2017 It's the way floating point arithmetic work, Basic Answers. There is a Decimal module. >>> from decimal import Decimal >>> Decimal('0.72') * Decimal('5') Decimal('3.60') RE: multiplying integer to decimal - ArnabRoyBatman - Jun-19-2017 So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programming RE: multiplying integer to decimal - buran - Jun-19-2017 in any case you would like to use string formatting when print float numbers. >>> x = 0.72*5 >>> x 3.5999999999999996 >>> '{:0.2f}'.format(x) '3.60' >>> 'the result of calculations is {:0.2f}'.format(x) 'the result of calculations is 3.60' >>> RE: multiplying integer to decimal - Ofnuts - Jun-19-2017 (Jun-19-2017, 07:03 AM)ArnabRoyBatman Wrote: So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programmingNo. Everyone (Python, Fortran, C, C#...) uses floating point, this is how rockets are sent to Saturn. Of course you have to learn to live with its idiosyncrasies. The cases where you want infinite precision are fairly rare (mostly when dealing with money). RE: multiplying integer to decimal - Larz60+ - Jun-19-2017 You can also use scaled calculations like >>> c = (72 * 5) / 100 >>> c 3.6 >>> RE: multiplying integer to decimal - Ofnuts - Jun-20-2017 (Jun-19-2017, 11:20 PM)Larz60+ Wrote: You can also use scaled calculations like This is Python, not assembler :) RE: multiplying integer to decimal - DeaD_EyE - Jun-20-2017 (Jun-19-2017, 07:03 AM)ArnabRoyBatman Wrote: So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programming This is not a Python problem. Please read what floating point is. It depends what you want. To do scientific calculations, you won't use decimal. For financial calculations you have to. |