12
How can I format a float
for the Brazilian value format(price)?
Example: in php number_format($float,2,',','.')
separates decimal with comma and unit of thousand with dot.
12
How can I format a float
for the Brazilian value format(price)?
Example: in php number_format($float,2,',','.')
separates decimal with comma and unit of thousand with dot.
12
If the formatting you want is used in a particular language, you can format your number by setting the Qlocate (English) with the language containing the desired formatting:
// a definição actual em uso
QLocale loc = QLocale::system();
// Recolher a formatação de números utilizada para a língua Portuguesa
QLocale brasil(QLocale::Portuguese);
loc.setNumberOptions(brasil.numberOptions());
// Define como valor por defeito
QLocale::setDefault(loc);
From here the formatting of numbers should already be as used by the Portuguese language.
It worked, thank you.
I’m having another problem. The values are being rounded. Example 11.020,51
showcase 11.020,50
. You know how I can add up the figures without rounding up?
@user628298 The best is to try to work with values in format inteiro
and not float
. As you have been asked here: http://answall.com/questions/5746/bestdatatypesshowto work with money
@Lucas Nunes I’ll take a look. Thank you.
@user628298 You can make use of the native functions of c++ ceil()
or floor()
. You can also use QString::number(sum, 'f', 1)
. See this documentation and this.
6
The solution using the standard C++ library looks like this:
First you need to create a specialization of the Std::numpunct class and overwrite two methods to implement the specific behavior for our currency.
#include <iostream>
#include <iomanip>
#include <locale>
using namespace std;
class BRL : public numpunct<char>
{
protected:
//separador de milhar
virtual char do_thousands_sep() const
{
return ',';
}
//padrão de agrupamento dos milhares, de 3 em 3
virtual std::string do_grouping() const
{
return "\03";
}
};
Then to use the following is done:
int main(int argc, char* argv[]){
float _valor = 1022.33;
//instanciando um locale que utilizará a especialização de numpunct
locale br(locale(), new BRL());
//configurar no cout o locale que será utilizado nas formatações
cout.imbue(br);
//setprecistion() é necessário para configurar precisão de apenas duas casas decimais
//fixed é necessário para que o valor não seja impresso como notação científica.
cout << "R$ " << setprecision(2) << fixed << _valor;
return 0;
}
The console output looks like this:
R$ 1,022.33
Full version on ideone.
In addition: You can see more options on how to change the parameters (comma to dot or as you prefer) in the class documentation: http://en.cppreference.com/w/cpp/locale/numpunct
Browser other questions tagged c++ qt
You are not signed in. Login or sign up in order to post.
You mean: use the dot as a thousand separator? (it doesn’t make much sense to use it as a ten separator)
– user4552
Dot by 3 dec(unit). In the topic informs.
– Hy-brazil
As I recall, the math was 3 tens = 30. I see no meaning and I do not know the meaning of "Point by 3 dozen (unity)" (what unity means in this context?).
– user4552
3 tens multiplied is equivalent to a thousand. This is the most widely used naming convention (in English it would be "thousands separator"). I edited the question to use this convention, if this is incorrect just reverse the edit.
– mgibsonbr
@mgibsonbr This correct! A unit of thousand or (unit).
– Hy-brazil