Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
str() with float an i18n
#1
as we know, some places use '.' and some places use ',' to separate the whole part of a number from the fractional part of a number. my curiosity question is whether the str() function uses the local preference, or if it just uses '.' everywhere.
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
here's the doc on that: https://docs.python.org/3.7/library/loca...LC_NUMERIC
Quote:locale.setlocale(category, locale=None)

If locale is given and not None, setlocale() modifies the locale setting for the category. The available categories are listed in the data description below. locale may be a string, or an iterable of two strings (language code and encoding). If it’s an iterable, it’s converted to a locale name using the locale aliasing engine. An empty string specifies the user’s default settings. If the modification of the locale fails, the exception Error is raised. If successful, the new locale setting is returned.

If locale is omitted or None, the current setting for category is returned.

setlocale() is not thread-safe on most systems. Applications typically start with a call of

import locale
locale.setlocale(locale.LC_ALL, '')

This sets the locale for all categories to the user’s default setting (typically specified in the LANG environment variable). If the locale is not changed thereafter, using multithreading should not cause problems.
I haven't actually tried it, but looks like it will accommodate numbers using LC_NUMERIC
'locale.localeconv()' Returns the database of the local conventions as a dictionary.
Leave testing to you.
Reply
#3
as long as str() does it, with whatever setup that is needed, if any, has been done, then i can "cheat" and just use str(1.5)[1] to get the right character.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,322 Jul-25-2019, 11:32 PM
Last Post: scidam

Forum Jump:

User Panel Messages

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