This question already has answers here:
How to convert exponent in Python and get rid of the 'e+'?
(2 answers)
Closed 3 months ago.
I want to convert numbers like 1.28e+21 to a long digits only number but the following code doesn't make a difference.
n = 1.28e+21 b = 1.28*10**21 print(b)
b still has an e.
How do I get rid of e?
These numbers in exponential format are from type float in python.You can use int to convert it to an integer.
>>> n = int(1.28e+21)
>>> n
1280000000000000000000
You can also use decimal module like this:
>>> import decimal
>>> decimal.Decimal(1.28e+21)
Decimal('1280000000000000000000')
>>>
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: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.
This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Closed 6 years ago.
Can someone tell me what I'm missing here? This is using Python 2.7.11:
print float(148.95)
print float(148.95)*100
print int(float(148.95)*100)
Why does this print:
148.95
14895.0
14894 <--- Shouldn't this be 14895?
148.95 is not a number that can be exactly represented using floating point. The number internally stored is actually 148.94999999999998863131622783839702606201171875. When you multiply by a hundred, you get 14894.999999999998181010596454143524169921875. When you convert that to integer, it cuts off the .999... and you're left with 14894.
If you want a data type that can exactly represent numbers with at least two decimal places of precision, consider using Decimal.
>>> from decimal import Decimal
>>> x = Decimal("148.95")
>>> print x
148.95
>>> print x*100
14895.00
>>> print int(x*100)
14895
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:
Print floating point values without leading zero
(13 answers)
Closed 8 years ago.
I have a simple question, that I feel should have a simple solution. How do I format a floating number so that only the numbers after the decimal point show? I would prefer to use '{}'.format to accomplish this.
>>> n = 0.12345
>>> n
0.12345
>>> str(n)[1:]
'.12345'
>>> '{}'.format(n)
'0.12345'
>>> '{}'.format(str(n)[1:])
'.12345'
I know I can use str(n)[1:], but I'd prefer not to have to convert the number to a string.
I do not think there is a format string which removes the zero. However, you could use lstrip:
In [25]: n = 0.12345
In [26]: '{:.3f}'.format(n).lstrip('0')
Out[26]: '.123'
At least that is safer than str(n)[1:], which would remove a signficiant digit if n were equal to a number bigger than 1 or less than -1.