help with Qlocale and Brazilian currency format

Asked

Viewed 319 times

-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!

  • 1

    Check if self.orcamentoAvailable is float or int

  • 1

    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 method toString. Some accept integer values. Vc is sure that their value in self.orcamentoDisponivel is represented as a real value? Anyway, it should work if you do: lang.toString(float(self.orcamentoDisponivel)).

  • 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.

  • self.orcamentoAvailable = integer.

  • I’m sorry, now you have the minimal example.

  • Well, the number 225710000 is quite different of the number 2257100. I repeat: it seems that you just forgot to divide by 100, just like you do there in C++. Try to change the call print lang.toString(orcamentoDisponivel) for print lang.toString(int(orcamentoDisponivel * 0.01)).

  • 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.

  • 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 of QLocale in both languages is exactly the same.)

  • 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

Show 4 more comments

1 answer

1


Solved:

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

or

#http://stackoverflow.com/questions/41917083/qlocale-and-brazil-currency-format    
from PyQt4 import QtCore
value = 225710000
lang = QtCore.QLocale('pt_BR')
print lang.toString(value * 0.01, 'f', 2)

Browser other questions tagged

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