Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# Convert float to decimal, wrong value

By : stacey91
Date : July 31 2020, 08:00 PM
Hope this helps I have a float number: 10071072872302 and I need to convert it to decimal. ,
I have a float number: 10071072872302.
code :

Share :

## MySql: convert a float to decimal produce more decimal number then the stored in back.sql file

By : user2379431
Date : March 29 2020, 07:55 AM
hope this fix your issue This is the difference between float and decimal. Float is a binary type, and can't represent that value exactly. So when you convert to decimal (as expected, a decimal type), its not exactly the original value.

## Django Cannot convert float to Decimal. First convert the float to a string

By : S.E. Laugs
Date : March 29 2020, 07:55 AM
To fix this issue As from official documentation 9.4.8. Decimal FAQ
code :
``````def float_to_decimal(f):
"Convert a floating point number to a Decimal with no loss of information"
n, d = f.as_integer_ratio()
numerator, denominator = Decimal(n), Decimal(d)
ctx = Context(prec=60)
result = ctx.divide(numerator, denominator)
while ctx.flags[Inexact]:
ctx.flags[Inexact] = False
ctx.prec *= 2
result = ctx.divide(numerator, denominator)
return result
``````

## Is it possible to predict when Perl's decimal/float math will be wrong?

By : user3440479
Date : March 29 2020, 07:55 AM
will be helpful for those in need None of this is Perl specific. See Goldberg:
Rounding Error

## How do I convert float decimal to float octal/binary?

By : Lionel Sausin
Date : March 29 2020, 07:55 AM
may help you . I have been searched everywhere to find a way to convert float to octal or binary. I know about the float.hex and float.fromhex. Is theres a modules which can do the same work for octal/binary values? , Here's the solution, explanation below:
code :
``````def ToLessThanOne(num): # Change "num" into a decimal <1
while num > 1:
num /= 10
return num

def FloatToOctal(flo, places=8): # Flo is float, places is octal places
main, dec = str(flo).split(".") # Split into Main (integer component)
# and Dec (decimal component)
main, dec = int(main), int(dec) # Turn into integers

res = oct(main)[2::]+"." # Turn the integer component into an octal value
# while removing the "ox" that would normally appear ([2::])
# Also, res means result

# NOTE: main and dec are being recycled here

for x in range(places):
main, dec = str((ToLessThanOne(dec))*8).split(".") # main is integer octal
# component
# dec is octal point
# component
dec = int(dec) # make dec an integer

res += main # Add the octal num to the end of the result

return res # finally return the result
``````

## Convert float to float and truncate after two decimal places Objective-C

By : Rhiannon Louise
Date : March 29 2020, 07:55 AM
To fix the issue you can do %f simply rounds for the output. Transforming a float value into a string and back does not work, if the exact value (i. e. 4.73) has no representation in the float format. So transforming it back will "round" the stored value 4.73 into the float format, which is obviously 4.730…02.
You should rarely use (binary, IEEE) floats for financial calculating. Financial values (amount of money) is in most cases an integral value of cents, but no float value of dollars (or whatever). Additionally you can think about using NSDecimal and NSDecimalNumber to ensure, that every value with two digits of precision is storable in the format.