Unwanted decimal in Python [duplicate] - python

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 8 years ago.
I have written a code that will add up the values in a tuple and calculate the average:
def average(values):
return sum(values[0:]) / len(values[0:])
However, I get an unwanted floating point, like 2.0 instead of 2. How do I eliminate this, but still manage to get the correct average should the average not be an integer?

You may try like this:
if (yournumber).is_integer():
print int(n)
else
print (n)

Related

Add leading zeros into binary [duplicate]

This question already has answers here:
Convert to binary and keep leading zeros
(10 answers)
Closed 2 years ago.
Hi i would like to add leading zeros into binary to make it to have 8 digits. I've tried using zfill() but it doesnt seem to work and i have no idea how to use format() cause all the answers i've found are all keeping the 0b which i dont want that.
Here's what i've tried:
lenToBin = bin(payloadLength).replace("0b", "")
if payloadLength == 30 or payloadLength == 31:
binResult = lenToBin.zfill(3)
else:
binResult = lenToBin.zfill(2)
Is this what you want:
lenToBin = bin(payloadLength).replace("0b", "").zfill(8)

How to round float in multiple of 5 [duplicate]

This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
How do I round to the nearest 0.5?
(10 answers)
Closed 2 years ago.
I got this number 1.12412 and I want to round it to 1.12415 or 1.12410 (muliple of 5 in last decimal)
If using the Round(X,4) function I get 1.1241 (4 decimals).
Is there a function that can make that happen?
Thanks!
There is an answer in stack but using c# not python
My way to do that is to specify rounding unit first and then simple trick as below:
import numpy as np
rounding_unit = 0.00005
np.round(1.12412/rounding_unit) * rounding_unit
You may:
Multiply your number by 2
Use Round(X,4)
Divide the result by 2
profit!!!

Remove decimal from float if it is equal to .0 [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 3 years ago.
I want to print a float value but if the value has no decimal (1.0, 2.0 etc, etc) I want to either remove the .0 part or convert it into an int so it removes the .0 anyway. Just for visual effect.
output:
You started with: 1.0
Wanted output:
You started with: 1
you can check if the number is a whole number or not
number = 1.5
if number.is_integer():
number = int(number)
print(number)

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

Padding zeroes to left of string (python)? [duplicate]

This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 7 years ago.
The length of the string needs to be 5 characters. When the string is "1" it needs to be returned as "00001", when the string is "10" it needs to be returned as "00010" and so on. I'm wondering how to do this using loops?
If you want to use for-loops, you can solve the problem like so:
def addPadding(str):
output = ''
# Prepend output with 0s
for i in range(5 - len(str)):
output += '0'
output += str
return output
print(addPadding('10'))
>> 00010
print(addPadding('1'))
>> 00001
If you can't use string formatting or arrays or anything besides integer operators, you should be able to figure it out using division and a loop.
Is 10 divisible by 10000?
Is 10 divisible by 1000?
Is 10 divisible by 100?
etc.
Try typing 10/10000 in your python interpreter. What's the result? :)

Categories