## Fix Python – Formatting floats without trailing zeros

How can I format a float so that it doesn’t contain trailing zeros? In other words, I want the resulting string to be as short as possible.
For example:
3 -> “3”
3. -> “3”
3.0 -> “3”
3.1 -> “3.1”
3.14 -> “3.14”
3.140 -> “3.14”

….

## Fix Python – Why does Python’s hash of infinity have the digits of π?

The hash of infinity in Python has digits matching pi:
>>> inf = float(‘inf’)
>>> hash(inf)
314159
>>> int(math.pi*1e5)
314159

Is that just a coincidence or is it intentional?
….

## Fix Python – Format / Suppress Scientific Notation from Pandas Aggregation Results

How can one modify the format for the output from a groupby operation in pandas that produces scientific notation for very large numbers?
I know how to do string formatting in python but I’m at a loss when it comes to applying it here.
df1.groupby(‘dept’)[‘data1’].sum()

dept
value1 1.192433e+08
value2 1.293066e+08
value3 1.077….

## Fix Python – How to check if a float value is a whole number

I am trying to find the largest cube root that is a whole number, that is less than 12,000.
processing = True
n = 12000
while processing:
n -= 1
if n ** (1/3) == #checks to see if this has decimals or not

I am not sure how to check if it is a whole number or not though! I could convert it to a string then use indexing to check the end va….

## Fix Python – Why are some float < integer comparisons four times slower than others?

When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude.
For example:
>>> import timeit
>>> timeit.timeit(“562949953420000.7 < 562949953421000") # run 1 million times 0.5387085462592742 But if the float or integer is made smaller or larger by a certain amount, the compariso....

## Fix Python – Python JSON serialize a Decimal object

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 accept Decimal objects, and converting to a float beforehand yields {‘x’: 3.8999999999999999} which….

## Fix Python – Convert floats to ints in Pandas?

I’ve been working with data imported from a CSV. Pandas changed some columns to float, so now the numbers in these columns get displayed as floating points! However, I need them to be displayed as integers or without comma. Is there a way to convert them to integers or not display the comma?
….

## Fix Python – How to get a random number between a float range?

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

## Fix Python – How do you round UP a number?

How does one round a number UP in Python?
I tried round(number) but it rounds the number down. Example:
round(2.3) = 2.0

and not 3, as I would like.
The I tried int(number + .5) but it round the number down again! Example:
int(2.3 + .5) = 2

….

## Fix Python – How can I force division to be floating point? Division keeps rounding down to 0?

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 Python 2 in the following? c = a / b ....