How to round float in multiple of 5 [duplicate] - python

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!!!

Related

F-string in Python ":.3f" [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
how to format float number in python? [duplicate]
(3 answers)
variable number of digit in format string
(3 answers)
Closed 1 year ago.
I'm reading this textbook called "Practical Statistics for Data Scientists" and this :.3f keeps getting used in almost every f-string. What does :.3f mean? My guess is it has something to do with floating point numbers.
Example:
{house_lm_factor.intercept_:.3f}
This is show you how many number are printing:
>>> import math
>>> flt = math.pi
>>> f'{flt:.3f}'
'3.142'
>>> f'{flt:.5f}'
'3.14159'
>>> f'{flt:.10f}'
'3.1415926536'

How ~12 gives -13 in decimal? [duplicate]

This question already has answers here:
What is “two's complement”?
(24 answers)
How to think about Python's negative number bitwise operations?
(2 answers)
Closed 1 year ago.
I have a question in my programming class, it is: What is the value of ~12 and the answer is -13. I don't understand why? I convert 13 base 10 to binary, which is 1100, than I switched all the 1 for 0 and vice versa, it gives me 0011, so I thought the answer was 3 but it's not.
Converting 12 to a signed binary number yields 01100 then assuming that ~ is supposed to invert every bit we get 10011 which can be converted from signed binary to decimal yielding -13.

how to do division in python without decimal points [duplicate]

This question already has an answer here:
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
if I use integer type cast conversion technique then it doesn't work for large numbers like 12630717197566440063
I got wrong answer in some cases like below in python 3
a =12630717197566440063;
print(a)
temp = a/10
print(int(temp))
Then I am getting 1263071719756644096 as a answer instead of 1263071719756644006
You can use the // (floor division) operator:
temp = a//10
print(temp)

What does "e" in "1e-5" in Python language mean and what is the name of this notation? [duplicate]

This question already has answers here:
What does the suffix e+number mean in python at the end of a float?
(2 answers)
What is the meaning of number 1e5?
(5 answers)
Closed 6 years ago.
I notice that there is such an expression "1e-5" in Python(probably in other languages also)
What is the name of this notation?
what does it denote in math?
What does 'e' mean? It's the first time I see a character helps to denote some value, are there other characters also help do so?
Why should use this way instead of some other python math operation like pow() etc.
It is scientific notation. It means 1 × 10−5. In other words, 0.00001.
10 ** -5, i.e. 10 to the power of negative 5, 1 divided by 10 to the power of 5, or 0.00001.
It means 10 to the power of -5 times 1

Unwanted decimal in Python [duplicate]

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)

Categories