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 Overflow
๐ŸŒ
Python
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.
Discussions

What's different between Decimal and Floating point numbers in Python?
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. More on reddit.com
๐ŸŒ r/learnpython
6
5
March 5, 2021
How to do Decimal to float conversion in Python? - Stack Overflow
I need to convert a Decimal value to float in Python. Is there any method available for this type conversion? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert dict floats to decimals
so you are asking if 1.23 is equal to... >>> type(float) the answer is obviously no. the kosher way to do the test is to use isinstance(value, desired_type) btw, what is your end game with that Decimal conversion? because i don't think you are gaining anything here >>> import decimal >>> decimal.Decimal(0.3) Decimal('0.299999999999999988897769753748434595763683319091796875') >>> decimal.Decimal('0.3') Decimal('0.3') long story short float is a bad seed for Decimal type More on reddit.com
๐ŸŒ r/learnpython
6
5
November 20, 2015
i want to convert a float(input) to print the decimal number only ignoring the whole
You could just divide by 100 X/100 50/100 = 0.50 More on reddit.com
๐ŸŒ r/learnpython
9
2
August 13, 2023
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ convert-decimal-float-python
Converting Decimal to Float in Python - Flexiple
March 22, 2024 - The โ€˜Converting Decimal to Float in Pythonโ€˜ blog explains various techniques to convert from decimal to float.
๐ŸŒ
The Teclado Blog
blog.teclado.com โ€บ decimal-vs-float-in-python
Decimal vs float in Python - The Teclado Blog
October 26, 2022 - When working with fractions in Python, we tend to use floats, but we also have the decimal module. Learn how to use it, and its benefits over floats.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-decimal-from_float-method
Python | Decimal from_float() method - GeeksforGeeks
May 23, 2022 - Decimal#from_float() : from_float() is a Decimal class method which converts a float to a decimal number, exactly.
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.
Find elsewhere
๐ŸŒ
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 ยท
๐ŸŒ
LAAC Technology
laac.dev โ€บ blog โ€บ float-vs-decimal-python
Float vs Decimal in Python | LAAC Technology
March 4, 2021 - Learn the differences between floats and decimals in Python, common issues, and when to use each.
๐ŸŒ
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.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert python float to decimal
5 Best Ways to Convert Python Float to Decimal - Be on the Right Side of Change
February 16, 2024 - A float might be 8.675309, but we require precision without floating-point arithmetic errors, thus the desired output is a Decimal such as Decimal(โ€˜8.675309โ€™). Method 1: Using the Decimal class from the decimal ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-convert-a-decimal-to-a-float-in-Python
How to convert a decimal to a float in Python - Quora
If speed is not a problem and you need greater precision compared to floating-point numbers then you can convert to a decimal number. ... To learn more about decimal module in python, check the below link and follow the space.
๐ŸŒ
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
๐ŸŒ
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')