-1
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(orcamentoDisponivel * 0.01, 'f', 2).toStdString();
in Pyqt, I did this:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore
orcamentoDisponivel = 225710000
loc = QtCore.QLocale.system().name()
lang = QtCore.QLocale(loc) # ou substituir loc por 'pt_BR'
print lang.toString(int(orcamentoDisponivel * 0.01))
The problem is that while in c++ I had as output, for example: 2.257.100,00 (correct value for my case)
in python I have as output: 225.710.000
Can someone help me sort this out? Thank you!
Check if self.orcamentoAvailable is float or int
– Bacco
It is hard to say for sure if this is the problem (how about you produce a [mcve] that reproduces the error, huh?), but the class
QLocale
has several different overloads of the methodtoString
. Some accept integer values. Vc is sure that their value inself.orcamentoDisponivel
is represented as a real value? Anyway, it should work if you do:lang.toString(float(self.orcamentoDisponivel))
.– Luiz Vieira
P.S.: Note also that in your implementation in C++ you multiply the value by 0.01 (which is equivalent to divide by 100). And you don’t do this multiplication in Python. The problem may be related to that too.
– Luiz Vieira
self.orcamentoAvailable = integer.
– Juliano Gomes
I’m sorry, now you have the minimal example.
– Juliano Gomes
Well, the number
225710000
is quite different of the number2257100
. I repeat: it seems that you just forgot to divide by 100, just like you do there in C++. Try to change the callprint lang.toString(orcamentoDisponivel)
forprint lang.toString(int(orcamentoDisponivel * 0.01))
.– Luiz Vieira
It really needed to be divided (thank you), but it still doesn’t show the two houses after the comma. ex: orcamentoDisponivel = 225710035. the output would be: 2.257.100.
– Juliano Gomes
Of course not. Integers do not have homes after the comma. : ) But you can format as
float
in the same way as in C++. Lack a little attention, huh? Just use the same code. (Note that the interface ofQLocale
in both languages is exactly the same.)– Luiz Vieira
And there Uiz, beauty? thanks for your contributions, I’ve solved the problem. A suggestion for you: you can help people without having to be grumpy. Hug and thanks novemante
– Juliano Gomes