This question already has answers here:
Convert decimal mark when reading numbers as input
(8 answers)
Closed 10 months ago.
I take the current exchange rate from the bank's website as a string and I want to convert this string into a number for further calculations and I want to do it as beautifully as possible.
How to convert the string 77,4651 $ to 77.4651 in float format without using func 'replace'?
Use float regular expression to make sure, that you get the float number
txt = "77.4651 $"
x = float(re.search("[-+]?[0-9]*(?:\.?[0-9]*)[1]", txt).string)
or less safe split by spaces
float("77.4651 $".split("\s+")[0])
Regex would be useful here to account for varying formats:
import re
float('.'.join(re.findall('[0-9]+', "77,4651 $")))
Related
This question already has answers here:
Splitting a number into the integer and decimal parts
(9 answers)
How to get numbers after decimal point?
(37 answers)
Closed 2 years ago.
If I have a float like 32.879, and I want to end up with just 0.879, I can think of a few ways to do it, like:
Convert to string, strip off everything before the ., convert back to float; OR
32.879 - int(32.879)
Both of those feel like hacks. Is there no pure math operation that can do this?
Sort of like using abs() instead of if x < 0: x = x*-1
I'm dealing with Python, but if anyone can tell me the math name for this operation, I can probably google the Python way to do it.
This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 4 years ago.
I am looking for an expression that will truncate a float to at most a certain number of digits. I want to preserve a certain number of decimals, without having unnecessary trailing 0s.
So, this almost works as desired:
"{0:3.f"}.format(number)
For input 3.123000001:
"{0:.3f}".format(3.1230000001)
'3.123'
Great. But for input 3:
"{0:.3f}".format(3)
'3.000'
I figured out the answer while I was writing the question. Just add .rstrip('0') to the expression. So:
"{0:3.f}".format(number).rstrip('0')
This question already has answers here:
how to format a date in shell script
(3 answers)
Closed 2 years ago.
What is the simplest way to convert a date formatted 3/2/2004 to 030204 in pure bash, or if not possible, then Python?
I need to at zeros in front of an single digit sectors of the date, remove the parenthesis, and have only the last two characters of the 4 digit year.
I know I could write an extensive Python script that would create an array splitting at /, and for any single digit arrays I would add a 0. I don't want to do this because it seems unnecessary. Thanks in advance for any help!
date -d'11/2/1998' +%m%d%y
110298
import datetime
d = datetime.datetime.strptime("11/2/1998", "%d/%m/%Y")
print d.strftime("%d%m%y")
This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 7 years ago.
I will give the example from Turkish, for example "şğüı" becomes "sgui"
I'm sure each language has it's own conversion methods, sometimes a character might be converted to multiple ASCII characters, like "alpha"/"phi" etc.
I'm wondering whether there is a library/method that achieves this conversion
What you are asking is called transliteration.
Try the Unidecode library.
This question already has answers here:
How do I convert a currency string to a floating point number in Python?
(10 answers)
Closed 7 years ago.
I've looked through the 'currency' threads, but they're all for going the other way. I'm reading financial data that comes in as $1,234.56 &c, with everything a string. I split the input line, and want to convert the value item to float for add/subtract (I'm mot worried about roundoff error). Naturally, the float() throws an error.
I could write a function to call as 'amount = float(num(value_string)), but woder if there's a "dollar_string_to_float()" function in one of the 32,000 Python modules.
I think this question is slightly different from this question, but I'm not sure.
Anyway, the code from the afformentioned question just need one function change from Decimal to Float and the removal of the Decimal import.
As you requested, the code is in a dollar_string_to_float function:
>>> from re import sub
>>> def dollar_string_to_float(s):
return float(sub(r'[^\d.]', '', money))
>>> money = '$1,234.56'
>>> print dollar_string_to_float(money)
1234.56
Look into the regular expressions module. You can compile a pattern that matches your dollars/cents format and extract the floating-point number from it.