Juliano Gomes
Juliano Gomes

Reputation: 77

Qlocale and brazil currency format

In c ++ I used to convert an integer value to the Brazilian currency format as follows:

QLocale loc = QLocale::system();
QLocale brasil(QLocale::Portuguese);
loc.setNumberOptions(brasil.numberOptions());
QLocale::setDefault(loc);

cout << brasil.toString(value * 0.01, 'f', 2).toStdString();

In PyQt, I did this:

# -*- coding: utf-8 -*-
from PyQt4 import QtCore
value = 225710000 #integer
lang = QtCore.QLocale('pt_BR')
print lang.toString(int(value * 0.01))

The problem is while in C ++ I had a output, for example: 2.257.100,00 (correct value for my case)

In python I have output: 225.710.000

Could someone help me solve this? Thank you!

Upvotes: 1

Views: 953

Answers (2)

Juliano Gomes
Juliano Gomes

Reputation: 77

Solved too with locale:

#https://docs.python.org/2/library/locale.html
import locale
locale.setlocale(locale.LC_ALL, '') 
print locale.format('%.2f', (value * 0.01), True)

Upvotes: 1

ekhumoro
ekhumoro

Reputation: 120768

To get the same output as the C++ example, you just need to pass the same arguments to toString:

>>> from PyQt4 import QtCore
>>> value = 225710000
>>> lang = QtCore.QLocale('pt_BR')
>>> print lang.toString(value * 0.01, 'f', 2)
2.257.100,00

Upvotes: 0

Related Questions