Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decimal comma
#1
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'.
Reply
#2
try:
>>> x = "5,8" >>> print(float(x.replace(',', '.'))) 5.8
BashBedlam likes this post
Reply
#3
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'.
Reply
#4
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'
Reply
#5
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'.
Reply
#6
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'.
Reply
#7
int is for integers only. No decimals. Perhaps you meant float()

>>> float('100_000.2') 100000.2
Reply
#8
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
Reply
#9
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'.
Reply
#10
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 2,392 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 2,396 Jun-13-2022, 10:11 PM
Last Post: dee
  Adding a comma in the resulting value stsxbel 6 4,646 May-22-2021, 09:24 PM
Last Post: stsxbel
  How to instantly add quotation marks and comma for parameters? cheers100 4 14,830 Oct-22-2020, 12:51 PM
Last Post: cheers100
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 6,701 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 7,445 Apr-10-2020, 02:39 PM
Last Post: buran
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 4,057 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck
  testing for Decimal w/o importing decimal every time Skaperen 7 7,183 May-06-2019, 10:23 PM
Last Post: Skaperen
  decimal point or comma Skaperen 8 8,896 Mar-17-2019, 06:02 AM
Last Post: DeaD_EyE
  string agg separated by comma shyamal_kesh 1 2,995 Nov-23-2018, 12:57 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.