In python, is there a way or even build in Function to convert a string into a float? Specifically:+1.488763E+01 into 14.88763 ?
What you have is a valid float literal. Convert it to a float, then convert the result back to a str
>>> '{:f}'.format(float('+1.488763E+01'))
'14.887630'
The format method is used to force the value to be represented as a fixed-point value, rather than in exponential notation. For example,
>>> str(float('1e20'))
'1e+20'
>>> '{:f}'.format(float('1e20'))
'100000000000000000000.000000'
Python does this conversion automatically. Just try it.
a = +1.488763E+01
print(a)
14.88763
It’s a decimal, it already is a float. Though if you print it, python will automatically put it in the format you want.
As long as you don’t have it as a string, it’ll be in the format you want it.
a = +1.488763E+01
print(a)
14.88763
You can use float() function
float(a)
Related
I have a problem, I would like to keep all decimals when converting string to float. How can I do this?
Each time, I have like a round() of values.
As you can see, the column named 'price_to_numeric' had less decimal than the column 'price'.
enter image description here
If you want to convert from string to float (a number with decimals) you can use the float() function.
To do this the string just needs to be passed into the function as follows: float('6.5') and the output of the function will be 6.5
If that is what you meant then enjoy, if not please upload the code for us to see.
I am using Scapy's sniff function for reading packets from a pcap file.
pkt=sniff(offline="a.pcap",count=1)[0]
In IDLE with pkt.time, I am able to get the time-stamp of the packet i.e. 1431063004.998014. But when I tried to convert the time-stamp to a string with str(pkt.time) or instead of pkt.time I give print pkt.time, I get only 1431063005.0.
Is it possible to get the exact time-stamp value as a string??
Note:
I looked in to decimal module. But that requires the precision number. That won't help without knowing the number of digits, I guess.
repr is another method like str which converts the whole float with precision. No need to import decimal or anything. Use it just like you use str and it returns a str object only.
>>> x = repr(pkt.time)
>>> type(x)
>>> <type 'str'>
Try this one:
"{:.6f}".format(pkt.time)
try this:
"{:.6f}".format(pkt.time)
What is the proper/accepted way to print and convert a numpy.float64 to a string? I've noticed just using print or str() will lose some precision. However, repr maintains the full precision. For example:
>>> import numpy
>>> print numpy.float64('6374.345407799015')
6374.3454078
>>> print repr(numpy.float64('6374.345407799015'))
6374.3454077990154
I assume that just calling print turns into calling str() on the float64 object. So is __str__() for numpy.float64 implemented with something like '%s' % (float(self)) or somehow casts the float64 with Python's built-in float()? I tried to quickly look around the numpy source for this but wasn't immediately obvious what was happening.
I've always thought repr() should return valid Python code that could be used by eval() to re-create the object. Is this an accepted convention? Luckily in this case numpy does not follow this convention because repr() returns just the raw number as a string instead of something like "numpy.float64('6374.345407799015')".
So, all of this confuses me. What is the correct way to convert a numpy.float64 to a string and/or print it while guaranteeing you always have the same, full precision?
The astype method works well:
>>> numpy.float64('6374.345407799015').astype(str)
'6374.345407799015'
Look into numpy.set_printoptions. Specifically,
numpy.set_printoptions(precision=15)
I want to use python to write code for another language which doesn't understand exponentially formatted floats. Is there an easy way to get python to, when converting floats to strings, use long-form notation (I.E. 0.000000009 instead of 9e-9)? I tried '%(foo)f', but it cuts the decimal short (0.00000).
Try something like
"%.16f" % f
This will still use exponential format if the number is too small, so you have to treat this case separately, for example
"%.16f" % f if f >= 1e-16 else "0.0"
Use a specific format specifier, e.g.:
>>> f=9*(10**-9)
>>> str(f)
'9e-09'
>>> "%.23f" % f
'0.00000000900000000000000'
UPDATE (thanks to #Sven): The amount of digits you want to use depends on the magnitude of the number. if you have large numbers (like several trillions) you won't need any decimals, obviously. for tiny numbers you need more. 'tis an ugly representation indeed.
Python provides a convenient method long() to convert string to long:
long('234')
; converts '234' into a long
If user keys in 234.89 then python will raise an error message:
ValueError: invalid literal for long()
with base 10: '234.89'
How should we a python programmer handles scenarios where a string with a decimal value ?
Thank you =)
longcan only take string convertibles which can end in a base 10 numeral. So, the decimal is causing the harm. What you can do is, float the value before calling the long. If your program is on Python 2.x where int and long difference matters, and you are sure you are not using large integers, you could have just been fine with using int to provide the key as well.
So, the answer is long(float('234.89')) or it could just be int(float('234.89')) if you are not using large integers. Also note that this difference does not arise in Python 3, because int is upgraded to long by default. All integers are long in python3 and call to covert is just int
Well, longs can't hold anything but integers.
One option is to use a float: float('234.89')
The other option is to truncate or round. Converting from a float to a long will truncate for you: long(float('234.89'))
>>> long(float('1.1'))
1L
>>> long(float('1.9'))
1L
>>> long(round(float('1.1')))
1L
>>> long(round(float('1.9')))
2L