Logo

Programming-Idioms

History of Idiom 146 > diff from v6 to v7

Edit summary for version 7 by programming-idioms.org:
[Python] 2 ways => 2 impl

Version 6

2016-09-17, 21:38:36

Version 7

2016-09-17, 21:39:03

Idiom #146 Convert string to floating point number

Extract floating point value f from its string representation s

Idiom #146 Convert string to floating point number

Extract floating point value f from its string representation s

Extra Keywords
real conversion
Extra Keywords
real conversion
Imports
import locale
Imports
import locale
Code
s = u'545.2222'
f = float(s)

s = u'545,2222'
locale.setlocale(locale.LC_ALL, 'de')
f = locale.atof(s)
Code
s = u'545,2222'
locale.setlocale(locale.LC_ALL, 'de')
f = locale.atof(s)
Comments bubble
when working with different locale decimal and thousand seperators you have to use locale.atof
Comments bubble
When working with different locale decimal and thousand separators you have to use locale.atof
Doc URL
https://docs.python.org/3/library/locale.html?highlight=local#module-locale
Doc URL
https://docs.python.org/3/library/locale.html?highlight=local#module-locale