Float to str conversion without losing two decimal places and changing decimal separator

Asked

Viewed 355 times

0

In Python, I need to convert a float to string and replace the point by comma, without losing the two decimal places. I have tried several ways, but could not.

In doing:

>>> str(10.00).replace('.',',')

The return is:

'10,0'

But I need it to be:

'10,00'

Does anyone have any idea how to do this in a practical way? That is, maintaining the format pattern with two decimal places after the comma.

I thought of creating a function, which would evaluate the amount of items after the comma, and if there was one, I would concatenate with zero, if there were two decimal numbers, I would do nothing. But I believe there is a more 'intelligent' way to do this in python, maybe something similar to what is done in some databases, example: to_char(10.00, '9999999999D99').

2 answers

2


If you are using IDLE may use the following code

>>> str(f'{float(10.00):.2f}').replace('.', ',')

In this case the exit will be:

'10,00'

Now if you want to implement a script you can use the following code:

n = float(input('Digite um valor real: '))
print(str(f'{n:.2f}').replace('.', ','))

OBSERVING

In the latter case the output will also be of the type string. If you ask to display the variable type the type will be displayed str.

2

One option is to use locale.format (up to Python 3.6), or locale.format_string (from Python 3.7 as in this version locale.format became deprecated):

import locale

locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')
print(locale.format_string('%.2f', 10.00)) # 10,00

First I use setlocale to indicate that I want to use the locale pt_BR (Brazilian Portuguese), where the decimal separator is the comma. Then, just indicate the format %.2f, that says to format the number with two decimal places.


The interesting thing about this module is that it allows a greater control over formatting, which I think is more appropriate than staying replace. For example, you can also indicate whether or not you will have the thousands separator, if the value is greater than 1000:

import locale

locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')

# sem separador de milhares
print(locale.format_string('%.2f', 12345.00)) # 12345,00

# com separador de milhares
print(locale.format_string('%.2f', 12345.00, True)) # 12.345,00

Besides being able to format as a monetary value (I don’t know if that was the intention):

import locale

locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')

# sem separador de milhares
print(locale.currency(12345.00)) # R$ 12345,00

# com separador de milhares
print(locale.currency(12345.00, grouping=True)) # R$ 12.345,00

The detail is that this module uses C libraries to get the format referring to the locale, according to the documentation:

The locale module is implemented on top of the _locale module, which in turn uses an ANSI C locale implementation if available.

Then the locale pt_BR must be installed/configured on the operating system for it to work. Otherwise, the alternative is to use replace even, as suggested in another answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.