It's well known that comparing floats for equality is a little fiddly due to rounding and precision issues.
For example:
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
What is the recommended way to deal with...

Asked By Gordon Wrigley

I have a number of strings similar to Current Level: 13.4 db. and I would like to extract just the floating point number. I say floating and not decimal as it's sometimes whole. Can RegEx do this or is there a better way?

Asked By Flowpoke

I have a number of strings similar to Current Level: 13.4 db. and I would like to extract just the floating point number. I say floating and not decimal as it's sometimes whole. Can RegEx do this or is there a better way?

Asked By Flowpoke

This problem is killing me. How does one roundup a number UP in Python?
I tried round(number) but it round the number down. Example:
round(2.3) = 2.0 and not 3, what I would like
The I tried int(number + .5) but it round the number down again! Example...

Asked By bodacydo

I have a list of floats. If I simply print it, it shows up like this:
[9.0, 0.052999999999999999, 0.032575399999999997, 0.010892799999999999, 0.055702500000000002, 0.079330300000000006]
I could use print "%.2f", which would require a for loop to traver...

Asked By static_rtti

randrange(start, stop) only takes integer arguments. So how would I get a random number between two float values?

Asked By Mantis Toboggan

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a.
How can I force c to be a floating point number in Pyt...

Asked By Nathan Fellman

Is there a way to step between 0 and 1 by 0.1?
I thought I could do it like the following, but it failed:
for i in range(0, 1, 0.1):
print i
Instead, it says that the step argument cannot be zero, which I did not expect.

Asked By Evan Fosmark

I want a to be rounded to 13.95.
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
The round function does not work the way I expected.

Asked By kevin

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31?
I just want to know how to parse a float str to a float, and (separately) an int str to an int.

Asked By Tristan Havelick

I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5 -> 5.00, 5.5 -> 5.50, etc). How can I do this in Python?

Asked By MarathonStudios

How do I get the numbers after a decimal point?
For example, if I have 5.55, how do i get .55?

Asked By l--''''''---------''''''''''''

I need to output 4 different floats to two decimal places.
This is what I have:
print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l'
Which is very unclean, and looks bad. Is there a way to make any float in that out ...

Asked By Omar

Suppose I have 8.8333333333333339, and I want to convert it to 8.84. How can I accomplish this in Python?
round(8.8333333333333339, 2) gives 8.83 and not 8.84. I am new to Python or programming in general.
I don't want to print it as a string, and the r...

Asked By hsinxh

I want to remove digits from a float to have a fixed number of digits after the dot, like:
1.923328437452 -> 1.923
I need to output as a string to another function, not print.
Also I want to ignore the lost digits, not round them.

Asked By Joan Venge

What is the best possible way to check if a string can be represented as a number in Python?
The function I currently have right now is:
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
Which...

Asked By Daniel Goldberg

The documentation for the round() function states that you pass it a number, and the positions past the decimal to round. Thus it should do this:
n = 5.59
round(n, 1) # 5.6
But, in actuality, good old floating point weirdness creeps in and you get:
5....

Asked By swilliams

I'm making a program that, for reasons not needed to be explained, requires a float to be converted into a string to be counted with len(). However, str(float(x)) results in x being rounded when converted to a string, which throws the entire thing off. Do...

Asked By Galileo

Basically, I'm converting a float to an int, but I don't always have the expected value.
Here's the code I'm executing:
x = 2.51
print("--------- 251.0")
y = 251.0
print(y)
print(int(y))
print("--------- 2.51 * 100")
y = x * 100
print(y)
print(int(y))...

Asked By B. Richard

I have a Decimal('3.9') as part of an object, and wish to encode this to a JSON string which should look like {'x': 3.9}. I don't care about precision on the client side, so a float is fine.
Is there a good way to serialize this? JSONDecoder doesn't acce...

Asked By Knio