Posts: 741 Threads: 122 Joined: Dec 2017 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 It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 12,117 Threads: 494 Joined: Sep 2016 try: >>> x = "5,8" >>> print(float(x.replace(',', '.'))) 5.8 BashBedlam likes this post Posts: 741 Threads: 122 Joined: Dec 2017 Feb-19-2022, 06:32 AM (This post was last modified: Feb-19-2022, 06:33 AM by DPaul.) 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 It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 4,874 Threads: 78 Joined: Jan 2018 Feb-19-2022, 07:57 AM (This post was last modified: Feb-19-2022, 07:57 AM by Gribouillis.) 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' Posts: 741 Threads: 122 Joined: Dec 2017 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)Output: 100_123.90
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 It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 741 Threads: 122 Joined: Dec 2017 Feb-19-2022, 08:41 AM (This post was last modified: Feb-19-2022, 08:42 AM by DPaul.) 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)Output: 100_123.90
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 It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 1,589 Threads: 3 Joined: Mar 2020 int is for integers only. No decimals. Perhaps you meant float() >>> float('100_000.2') 100000.2 Posts: 4,874 Threads: 78 Joined: Jan 2018 Feb-19-2022, 09:03 AM (This post was last modified: Feb-19-2022, 09:04 AM by Gribouillis.) 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 Posts: 741 Threads: 122 Joined: Dec 2017 Feb-19-2022, 09:05 AM (This post was last modified: Feb-19-2022, 09:08 AM by DPaul.) OK, understood. Allowed : int(100.55) Allowed : int('100') Not allowed : int('100.55') Allowed: float('100.55') thx, Paul bowlofred likes this post It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 2,171 Threads: 12 Joined: May 2017 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 |