Python <2.7
"%.15g" % f
Or in Python 3.0:
format(f, ".15g")
Python 2.7+, 3.2+
Just pass the float to Decimal constructor directly, like this:
from decimal import Decimal
Decimal(f)
Answer from jfs on Stack OverflowPython
docs.python.org โบ 3 โบ library โบ decimal.html
decimal โ Decimal fixed-point and floating-point arithmetic
And, in many applications, data is converted to Decimal with a single cast inside a loop. With context set and decimals created, the bulk of the program manipulates the data no differently than with other Python numeric types. ... Construct a new Decimal object based from value. value can be an integer, string, tuple, float, or another Decimal object.
Top answer 1 of 12
87
Python <2.7
"%.15g" % f
Or in Python 3.0:
format(f, ".15g")
Python 2.7+, 3.2+
Just pass the float to Decimal constructor directly, like this:
from decimal import Decimal
Decimal(f)
2 of 12
40
I suggest this
>>> a = 2.111111
>>> a
2.1111110000000002
>>> str(a)
'2.111111'
>>> decimal.Decimal(str(a))
Decimal('2.111111')
Videos
02:21
021025 COSC 1336 Python float(decimal) conversion - YouTube
17:00
Python's decimals SOLVE the floating point problem! - YouTube
Decimal Module in Python For Accurate Floats
01:51
Understanding the Conversion of String-Formatted Decimals to float ...
How to convert the integer to float in Python - YouTube
10:33
How To Convert String To Float In Python - YouTube
Reddit
reddit.com โบ r/learnpython โบ what's different between decimal and floating point numbers in python?
r/learnpython on Reddit: What's different between Decimal and Floating point numbers in Python?
March 5, 2021 -
I'm confused in differentiating between these two types of numbers. I'm a newbie.
Can anyone here please explain this?
Top answer 1 of 4
9
Integers (int type) are "whole numbers", without the fractional part, eg. 10. You use this type when the value is always an integer, e.g. a counter. Floating point numbers (float type) can have fractional part, e.g. 10.1234. They are stored using the closest representation in binary notation. Most numbers cannot be represented accurately, so they are slightly off. You use float for most mathematical calculations involving fractional numbers. Decimal numbers (Decimal type in Python) are used to represent floating point numbers accurately, with a defined precision (a defined number of places after decimal point). They are represented with two integer numbers, one for the integer part and one for the fractional part. For example. 10.1234 is stored as (10, 1234). Decimal type is required when fractional numbers must be represented accurately, with defined precision. The most notable example is financial calculations. Using float type you may get a result of a financial operation as 1010.123456$. But money is expressed with at most two decimal places. What does 0.123456$ mean? You can round it to 1010.12$, but then what happens with the remaining 0.003456$? Some "smart" programmers used that to their advantage in the past and they made a lot of money (which they eventually had to give back). So, for money calculations, you should use Decimal type. A good explanation of the Decimal type is in the documentation: https://docs.python.org/3/library/decimal.html
2 of 4
7
Python's integers (ยฑ0, 1, 2, etc.) have unlimited accuracy, which is unusual as far as programming languages go. In most languages they usually cap at 64-bits (aka long int) and there may be multiple integer types of various sizes. This makes Python very useful for scientific computing as you don't need a separate BigNum library to handle arbitrary precision. Python's floating-point numbers don't quite have this same luxury due to the inaccurate nature of them. Since many decimal numbers cannot be accurately represented in binary, floating-point math will always run into inaccuracies. For instance, 0.1 + 0.1 + 0.1 isn't 0.3, but something like 0.3000000000001. You'll use floats whenever whole numbers aren't enough, but you don't need a specific amount of accuracy. You probably never meant to ask about this, but decimal.Decimal is an alternative to float that lets you set its precision yourself. It's still not infinitely accurate, but it's often used in scientific computing where integers just can't cut it.
Luc
anh.cs.luc.edu โบ python โบ hands-on โบ 3.1 โบ handsonHtml โบ float.html
1.14. Decimals, Floats, and Floating Point Arithmetic โ Hands-on Python Tutorial for Python 3
Floating point numbers like 12.345 are a basic type, but there are some complications due to their inexactness. This section may be deferred until you actually need numbers other than integers. As you moved on in school from your first integer division to fractions and decimals, you probably thought of 6/8 as a fraction and could convert to a decimal .75. Python ...
TestDriven.io
testdriven.io โบ tips โบ c1f6f393-5fa2-4edd-8b64-cc1344219173
Tips and Tricks - Decimal vs float in Python | TestDriven.io
Python tip: You can use Decimal instead of float to: incorporate a notion of significant places (1.20 + 1.30 = 2.50) represent decimal numbers exactly (1.1 + 2.2 = 3.3) In short, use Decimal when precision matters. An example ๐ ยท from decimal import Decimal print(1.20 + 1.30) # => 2.5 print(Decimal("1.20") + Decimal("1.30")) # => 2.50 print(1.1 + 2.2) # => 3.3000000000000003 print(Decimal("1.1") + Decimal("2.2")) # => 3.3 ยท
Devcamp
basque.devcamp.com โบ pt-full-stack-development-javascript-python-react โบ guide โบ how-to-convert-between-integer-float-decimal-complex-numeric-data-types-python
How to Convert Between the Integer, Float, Decimal and Complex Numeric Data Types in Python
So in order to do that the keyword is float and I can say quantity. And now let's see what it does for us if I hit return. You can see 450 gets turned into 450.0 for if you ever need to get that. Now if you are working with the decimal library which we've worked with than that already that decimal function already converted a float into a decimal.
PythonHow
pythonhow.com โบ how โบ limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
To limit a float to two decimal points in Python, you can use the round function. The round function takes a number and the number of decimal points to round to as arguments, and returns the rounded number.
GitHub
gist.github.com โบ jackiekazil โบ 6201722
How do I round to 2 decimals in python? ยท GitHub
All the examples use demical types, except for the original value, which is automatically casted as a float. To set the context of what we are working with, let's start with an original value. ... from decimal import Decimal # First we take a float and convert it to a decimal x = Decimal(16.0/7) # Then we round it to 2 places output = round(x,2) print output
Top answer 1 of 3
90
There are two methods:
float_number = float(decimal_number)float_number = decimal_number * 1.0
First one using the built in function and second one without any function, but take care about the 1.0 not 1 as it should be float so the result will be float.
2 of 3
55
Just use float:
float_number = float(decimal_number)
Python Module of the Week
pymotw.com โบ 2 โบ decimal
decimal โ Fixed and floating point math - Python Module of the Week
Decimal operators also accept integer arguments, but floating point values must be converted to Decimal instances. $ python decimal_operators.py a = 5.1 b = 3.14 c = 4 d = 3.14 a + b = 8.24 a - b = 1.96 a * b = 16.014 a / b = 1.624203821656050955414012739 a + c = 9.1 a - c = 1.1 a * c = 20.4 a / c = 1.275 a + d = unsupported operand type(s) for +: 'Decimal' and 'float'
ZetCode
zetcode.com โบ python โบ decimal
Python Decimal - high-precision calculations in Python with Decimal
It is possible to change the default precision of the Decimal type. In the following example, we also use the mpmath module, which is a library for arbitrary-precision floating-point arithmetic. ... We need to install mpmath first. ... #!/usr/bin/python from decimal import Decimal, getcontext import math import mpmath getcontext().prec = 50 mpmath.mp.dps = 50 num = Decimal(1) / Decimal(7) num2 = mpmath.mpf(1) / mpmath.mpf(7) print(" math.sqrt: {0}".format(Decimal(math.sqrt(num)))) print("decimal.sqrt: {0}".format(num.sqrt())) print(" mpmath.sqrt: {0}".format(mpmath.sqrt(num2))) print('actual value: 0.3779644730092272272145165362341800608157513118689214')