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 Overflowfloating point - C# float to decimal conversion - Stack Overflow
floating point - C# How to convert between Float and Decimal? - Stack Overflow
How do you convert from IEEE 754 Floating Point to decimal without using a calculator?
convert an int to a float while preserving the float's original ending decimal value
How do I convert a number to floating-point?
To convert a decimal number to a floating-point representation, follow these steps:
- Convert the entire number to binary and then normalize it, i.e. write it in scientific notation using base 2.
- Truncate the fraction after the decimal point to the allowable fraction length.
- Extract the sign, exponent, and fraction segments.
Refer to the IEEE754 standard for more detailed instructions.
What is an IEEE754 floating-point number?
In computing, a floating-point number is a data format used to store fractional numbers in a digital machine. A floating-point number is represented by a series of bits (1s and 0s). Computers perform mathematical operations on these bits directly instead of how a human would do the math. When a human wants to read the floating-point number, a complex formula reconstructs the bits into the decimal system.
Why do we use floating-point numbers?
The IEEE754 floating-point format standard enables efficient storage and processing of numbers in computers.
-
From a hardware perspective, many simplifications of floating-point operations can be made to significantly speed up arithmetic, thanks to the IEEE754 standard's specifications.
-
For software, floats are very precise and typically lose a few millionths (if not less) per operation, which enables high-precision scientific and engineering applications.
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)
I suggest this
>>> a = 2.111111
>>> a
2.1111110000000002
>>> str(a)
'2.111111'
>>> decimal.Decimal(str(a))
Decimal('2.111111')
It's too late, the 8th digit was lost in the compiler. The float type can store only 7 significant digits. You'll have to rewrite the code, assigning to double or decimal will of course solve the problem.
This may be a compiler bug because it seems like a valid float should convert directly to a decimal. but it wont without losing resolution. Converting 125.609375 from float to decimal will lose resolution. However, converting it from float to double and then double to decimal will keep resolution.
float float_val = 125.609375f;
decimal bad_decimal_val = (decimal)float_val; //125.6094
double double_val = (double)float_val;
decimal good_decimal_val = (decimal)double_val;
The Convert class has static methods for converting between most primitive types.
To convert from float to decimal you can use Convert.ToDecimal():
float f = 1.0f;
decimal d = Convert.ToDecimal(f);
To go the other way you can use Convert.ToSingle():
decimal d = 1.0m;
float f = Convert.ToSingle(d);
One way is just to cast:
float a = 2.3F;
decimal d = (decimal)a;
Alternatively you could use a decimal constructor:
decimal d = new decimal(a);
We aren't allowed calculators in my upcoming exam, but are also required to convert IEEE 754 Floating Points with long mantissas into decimal.
The lecture slides only show examples where it's easy to work out, but I've gotten to a point where I need to convert 0.0000010011101 into decimal and this is simply impossible without a new approach.