decimal comma - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: decimal comma (/thread-36426.html) |
decimal comma - DPaul - Feb-18-2022 Hi , I need to convert "5,8" to a float = 5.8 Reading on this subject proposes 3 solutions - Adapt the locale - Do a replace (',','.') (if no 1000 separators) - Regex... I wonder if in the latest python releases something better has been proposed, or what would be the preferred solution? thx, Paul RE: decimal comma - Larz60+ - Feb-18-2022 try: >>> x = "5,8" >>> print(float(x.replace(',', '.'))) 5.8 RE: decimal comma - DPaul - Feb-19-2022 OK Larz, that is the on I chose as well. The only replace caveat is when there are also 1000 "." separators in the number. (like "100.000,55") Then you have to do a merry-go-round execise in 2 or 3 steps. thx, Paul RE: decimal comma - Gribouillis - Feb-19-2022 You could use str.translate() >>> s = "100.123.654,90" >>> s.translate({ord('.'): ',', ord(','): '.'}) '100,123,654.90'or >>> table = {ord('.'): '', ord(','): '.'} >>> s = "100.123.654,90" >>> s.translate(table) '100123654.90' RE: decimal comma - DPaul - Feb-19-2022 Translate function: nice! If I wanted to keep a 1000 separator: s1 = "100.123,90" trad = {44:46,46:95} x = s1.translate(trad) print(x) But now I'm in trouble, because python will do print(int('100_000') + int('100_000'))but it won't do: print(int(x) + int('100_000'))=> ValueError: invalid literal for int() with base 10: '100_123.90' Any obvious rea RE: decimal comma - DPaul - Feb-19-2022 Translate function: nice! If I wanted to keep a 1000 separator: s1 = "100.123,90" trad = {44:46,46:95} x = s1.translate(trad) print(x) But now I'm in trouble, because python will do print(int('100_000') + int('100_000'))but it won't do: print(int(x) + int('100_000'))=> ValueError: invalid literal for int() with base 10: '100_123.90' any obvious reason why? Paul RE: decimal comma - bowlofred - Feb-19-2022 int is for integers only. No decimals. Perhaps you meant float() >>> float('100_000.2') 100000.2 RE: decimal comma - Gribouillis - Feb-19-2022 You can also use the higher level function maketrans() to build the table. >>> table = str.maketrans(',', '.', '.') >>> s = "100.123.654,90" >>> s.translate(table) '100123654.90' >>> x = float(s.translate(table)) # compose with float ! >>> x 100123654.9 RE: decimal comma - DPaul - Feb-19-2022 OK, understood. Allowed : int(100.55) Allowed : int('100') Not allowed : int('100.55') Allowed: float('100.55') thx, Paul RE: decimal comma - DeaD_EyE - Feb-22-2022 It looks like i18n/l10n stuff. You can use locale to delocalize language-specific strings. import locale # get current systems locale # and set it for the current running interpreter locale.setlocale(locale.LC_ALL, locale.getlocale()) # input str from somewhere value_str = "1.200.500,33" # delocalize the value # converting the resulting str into a float value_float = float(locale.delocalize(value)) # format a float to a str with the current currency value_currency = locale.currency(value_float)The package babel has additional helper functions: https://babel.pocoo.org/en/latest/numbers.html |