c++ – Format decimal with comma and thousands with dot

Question:

How can I format a float to the Brazilian value(price) format?

Example: in php number_format($float,2,',','.') separates the decimal with comma and the unit of thousand with dot.

Answer:

Use QLocale

If the formatting you want is used in a certain language, you can format your number by setting Qlocate (English) to the language that contains 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 on, the formatting of numbers should already be as used by the Portuguese language.

Scroll to Top