Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decimal point or comma
#1
rather than deal with the complexity of internationalization where i need to have my own code fill in the decimal point or use decimal scaling separators (i never knew what to call it where "12345678" is made to look like "12,345,678") i am thinking of doing:
    decpnt = str(1.5)[1]
    decsep = ',' if decpnt=='.' else '.'
does this seem reasonable?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Only if I understand what you were asking. Is the question, where the decimal point should be placed? Or is the question have i placed my point at the right index?
Reply
#3
neither. it is whether the code is reasonable to determine which characters are being used in which place. is it ',' and '.' or '.' and ',' (or maybe even something else)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Mar-12-2019, 07:45 PM)Skaperen Wrote: where "12345678" is made to look like "12,345,678")
>>> f'{12345678:_}'
'12_345_678'
Skaperen Wrote:or maybe even something else
PEP 515 Wink
>>> 10_500 + 25_123
35623
>>> 10_000_000 + 5
10000005
>>> f'{1000000000:_}'
'1_000_000_000'
Reply
#5
If you do real i18n, not toy applications, you should avoid building your own stuff.
For i18n support: locale, gettext and hopefully other tools are used.
gettext is very verbose. There are better tools.

For l10n there are graphical tools for translators. They load the po file, save the translations as pot (if I remind right) and then they give it back to the programmer. The programmer runs msgfmt to create the binary blobs for the domain.

To implement it by your own, you have to know much about languages. Russian people have different forms of plural.
Then every country has a different symbol for currency. Different date formats and timezones (pytz).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
and different countries group digits in different ways, too. many countries group decimal digits in 4s, while India and Pakistan group by 2s after an initial group of 3.

if you look more closely at my code you can see that it bases things on what is already. i could do more of that as needed. line 1 gets the character used for decimal points so i can pass it to the .split() method. i did not see an i18n way to do that split.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
you can use this function

Total = 12345678
print("Total cost is: {:,.2f}".format(Total)) 
the result will be look like below
12,345,678.00
Reply
#8
will it do the right thing when the internationalization shows that it is in other places where the right way looks like "12.345.678,00" or "1234.5678,00" or "1,23,45,678.00"?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Here are my tries to work with locale and gettext.
In other words: It's pain in the a....


from pathlib import Path
import locale
from locale import (
    LC_ALL,
    LC_NUMERIC,
    setlocale,
    getlocale,
    getdefaultlocale,
    currency
    )
from locale import format_string as lformat
import gettext

def translations(domain, localedir):
    localedir = Path(localedir)
    result = {}
    for trans in localedir.glob(f'**/{domain}.mo'):
        lang = trans.parent.parent.name
        print(f'Found translation {trans}. Language: {lang}')
        try:
            result[lang] = gettext.translation(domain, localedir, [lang])
        except Exception as e:
            print(e)
    print()
    return result

trans = translations('messages', 'translations')


def current_setting():
    for symbol in dir(locale):
        if symbol.startswith('LC_') and symbol != 'LC_ALL':
            tp = getattr(locale, symbol)
            print(f'{symbol:<20}{locale.getlocale(tp)}')
    print()

def test():
    current_setting()
    try:
        print(currency(val))
    except ValueError as e:
        print(e)
    print(lformat('%.2f', val, grouping=True))
    print(_('Hello World!'))
    print()

val = 1337.42
defaultlocale = getdefaultlocale()
print('Defaultlocale is', defaultlocale)
print()

setlocale(LC_ALL, 'en_US.utf8')
trans['en_US'].install()
test()

setlocale(LC_ALL, 'en_US.utf8')
print('LC_ALL set to en_US.utf8')
trans['en_GB'].install()
test()

trans['de_DE'].install()
ret = setlocale(LC_ALL, defaultlocale)
print('LC_ALL set to', ret)
test()
Output
Output:
Found translation translations/en_US/LC_MESSAGES/messages.mo. Language: en_US Found translation translations/en_GB/LC_MESSAGES/messages.mo. Language: en_GB Found translation translations/de_DE/LC_MESSAGES/messages.mo. Language: de_DE Defaultlocale is ('de_DE', 'UTF-8') LC_COLLATE ('en_US', 'UTF-8') LC_CTYPE ('en_US', 'UTF-8') LC_MESSAGES ('en_US', 'UTF-8') LC_MONETARY ('en_US', 'UTF-8') LC_NUMERIC ('en_US', 'UTF-8') LC_TIME ('en_US', 'UTF-8') $1337.42 1,337.42 Fuck You! LC_ALL set to en_US.utf8 LC_COLLATE ('en_US', 'UTF-8') LC_CTYPE ('en_US', 'UTF-8') LC_MESSAGES ('en_US', 'UTF-8') LC_MONETARY ('en_US', 'UTF-8') LC_NUMERIC ('en_US', 'UTF-8') LC_TIME ('en_US', 'UTF-8') $1337.42 1,337.42 Hello World! LC_ALL set to de_DE.UTF-8 LC_COLLATE ('de_DE', 'UTF-8') LC_CTYPE ('de_DE', 'UTF-8') LC_MESSAGES ('de_DE', 'UTF-8') LC_MONETARY ('de_DE', 'UTF-8') LC_NUMERIC ('de_DE', 'UTF-8') LC_TIME ('de_DE', 'UTF-8') 1337,42 € 1.337,42 Hallo Welt!
I could not test with locale en_GB, because I haven't installed it on my system.
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 1,087 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,337 Jun-13-2022, 10:11 PM
Last Post: dee
  decimal comma DPaul 9 2,215 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  Adding a comma in the resulting value stsxbel 6 2,591 May-22-2021, 09:24 PM
Last Post: stsxbel
  How to instantly add quotation marks and comma for parameters? cheers100 4 7,918 Oct-22-2020, 12:51 PM
Last Post: cheers100
  Printing digits after the decimal point one by one uberman321 1 1,702 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,741 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 3,931 Apr-10-2020, 02:39 PM
Last Post: buran
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,525 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 2,618 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020