How to convert numbers to local currency format?
-
wrote on 21 Dec 2019, 23:11 last edited by
Hello friends.
I am trying to convert typed numbers in a line edit to local currency format.
I tried as in the QT documentation, but it didn't work. The compiler does not recognize NUMBER QMT TYPE.
It also does not recognize the variable type VAR, as shown in the example QT documentation.
I copied below what is in the documentation:
Method Documentation
string fromLocaleString(locale, number)
Returns a Number by parsing number using the conventions of the supplied locale.
If locale is not supplied the default locale will be used.
For example, using the German locale:Method Documentation
string fromLocaleString(locale, number)
Returns a Number by parsing number using the conventions of the supplied locale.
If locale is not supplied the default locale will be used.
For example, using the German locale:<code>
var german = Qt.locale("de_DE");
var d;
d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1234.56") // throws exception
d = Number.fromLocaleString(german, "1.234") // d == 1234.0<code>
Do I need to do some INCLUDE? Is this method obsolete? What is the most practical method to do this?
-
-
wrote on 23 Dec 2019, 11:59 last edited by
@SGaist I did not do this.
Where in the code should I put this "import"?
I tried to put in the .pro file but it didn't work. QT did not recognize this command.
-
At the top of your .qml file as explained here in Qt's documentation.
-
At the top of your .qml file as explained here in Qt's documentation.
wrote on 26 Dec 2019, 00:52 last edited by@SGaist I forgot to say that I am programming in C ++. I think the .qml file is not accepted here, right?
-
@Alexandre-Camelo said in How to convert numbers to local currency format?:
I forgot to say that I am programming in C ++.
Your examples above ar qml, now you're telling us you're trying to compile qml code in C++??
-
@Alexandre-Camelo said in How to convert numbers to local currency format?:
I forgot to say that I am programming in C ++.
Your examples above ar qml, now you're telling us you're trying to compile qml code in C++??
wrote on 31 Dec 2019, 23:40 last edited by@Christian-Ehrlicher Excuse me. My mistake. I'm new to QT.
I think I looked in the wrong part of the documentation.
The above example was copied from the QT documentation.
I am programming in C ++.
What is the method for converting numbers to local currency so that these numbers, even with currency formatting, can be used in mathematical expressions (sum, multiplication, etc.)?
-
Use QLocale
-
@Christian-Ehrlicher Excuse me. My mistake. I'm new to QT.
I think I looked in the wrong part of the documentation.
The above example was copied from the QT documentation.
I am programming in C ++.
What is the method for converting numbers to local currency so that these numbers, even with currency formatting, can be used in mathematical expressions (sum, multiplication, etc.)?
wrote on 1 Jan 2020, 17:02 last edited by VRonin 1 Feb 2020, 13:51@Alexandre-Camelo said in How to convert numbers to local currency format?:
I am programming in C ++.
as @Christian-Ehrlicher say, you have to used QLocale, like this:
auto german = QLocale("de_DE"); double value = 1234.56; qDebug() << german.toString(value, 'f', 2);
-
Hello friends.
I am trying to convert typed numbers in a line edit to local currency format.
I tried as in the QT documentation, but it didn't work. The compiler does not recognize NUMBER QMT TYPE.
It also does not recognize the variable type VAR, as shown in the example QT documentation.
I copied below what is in the documentation:
Method Documentation
string fromLocaleString(locale, number)
Returns a Number by parsing number using the conventions of the supplied locale.
If locale is not supplied the default locale will be used.
For example, using the German locale:Method Documentation
string fromLocaleString(locale, number)
Returns a Number by parsing number using the conventions of the supplied locale.
If locale is not supplied the default locale will be used.
For example, using the German locale:<code>
var german = Qt.locale("de_DE");
var d;
d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1234.56") // throws exception
d = Number.fromLocaleString(german, "1.234") // d == 1234.0<code>
Do I need to do some INCLUDE? Is this method obsolete? What is the most practical method to do this?
wrote on 2 Jan 2020, 13:52 last edited by@Alexandre-Camelo said in How to convert numbers to local currency format?:
I am trying to convert typed numbers in a line edit to local currency format.
double num = lineEdit->locale().toDouble(lineEdit->text());
-
@Alexandre-Camelo said in How to convert numbers to local currency format?:
I am programming in C ++.
as @Christian-Ehrlicher say, you have to used QLocale, like this:
auto german = QLocale("de_DE"); double value = 1234.56; qDebug() << german.toString(value, 'f', 2);
wrote on 2 Jan 2020, 22:42 last edited by@KroMignon Ok. It worked.
Thank you.
But I only have one more small question:
I read from the QT documentation that there is a way for QLocale to include the local currency symbol in the formatted number. Although I read, I couldn't understand how to do that.
After your tip, I was able to format the numbers to the local currency, but I'm concatenating the Brazilian currency symbol next to the number.
How can I enter the currency symbol through QLocale?
-
@Alexandre-Camelo said in How to convert numbers to local currency format?:
How can I enter the currency symbol through QLocale?
By taking a look at the documentation
-
@Alexandre-Camelo said in How to convert numbers to local currency format?:
How can I enter the currency symbol through QLocale?
By taking a look at the documentation
wrote on 4 Jan 2020, 00:32 last edited by
1/13