This question already has answers here:
Easy pretty printing of floats?
(19 answers)
Closed 5 years ago.
Very quick question.
x = 10
print("value is {:d}".format(x))
returns
value is 10
on the other hand:
x = 10.0
print("value is {:d}".format(x))
returns
ValueError: Unknown format code 'd' for object of type 'float'
Why doesnt this work?
You would use f not d for floats. And then specify the precision width as 0:
>>> print("value is {:.0f}".format(x))
value is 10
From Python docs: 'd' Decimal Integer. Outputs the number in base 10.
It will output the number in base 10, thats why you are getting the ValueError.
Related
This question already has answers here:
Python: Strip off string quotes from binary number
(5 answers)
Closed 1 year ago.
b = 15
a = bin(b) # I want return as an integer not string
print(a, type(a)) # output is string, actually I want is integer
# output - 0b1111 <class 'str'>
So, I want to get bin() function return as an integer
The int function is used to convert to the integer. We need to pass the number and its base to convert it into an integer (since, the base for binary values is 2).
a = int('101',2)
print(a)
If you question is about converting for example 5 into bin in python, the bin function actually gives 0b101 as the result. So the simple trick to get 101 as an int is 👇
intnum=int(bin(number)[2:])
This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 5 years ago.
def height(cent):
height= {}
height["feet"]= int(cent/30.48)
height["inch"]= int(cent%30.48)/2.54
print (height)
height (182.88)
print (182.88/30.48)
print (182.88%30.48)
The output is:
{'inch': 11, 'feet': 6}
6.0
30.479999999999993
Why does 182.88%30.48 not equal zero?
Because the value of 30.48 is really 30.4799.. This is because of the way that floating point numbers are stored in python. So when you are dividing 30.479999 by 182.88, the resulting rounded integer is 5 (i.e. 182.88 // 30.48 == 5). So the remainder is 30.47999...
This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'
This question already has answers here:
Integer square root in python
(14 answers)
Closed 9 years ago.
I am trying to get the square root of a really large number yet I get the error:
deltaSqrt = pow(delta,0.5)
OverflowError: long int too large to convert to float
In my case delta is equal to:
5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216
What should I do to get the square root of this number?
Use decimal:
import decimal
>>> d = decimal.Decimal('5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216')
>>> d.sqrt()
Decimal('7.139694782779097001143800270E+307')
If nothing else works, try this:
http://code.google.com/p/gmpy/
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 6 years ago.
In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back?
Is there a different method to get int/int = int?
Try this:
a = 1
b = 2
int_div = a // b