Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLocale toCurrencyString() reverse



  • I would like to know how to convert string with currency (i.e. obtained from QLocale().toCurrencyString(value)) back to double value.
    Note that in some OS currency string is sometimes "$1,000.00" or can be "1.000,00 €". How do I convert it to double?


  • Lifetime Qt Champion

    Hi,

    You can try with something like:

    QString currencyString = locale.toCurrencyString(1000.55);
    QString cleanedString = currencyString.remove(locale.currencySymbol()).trimmed();
    qDebug() << locale.toDouble(cleanedString);
    

    Hope it helps



  • This post is deleted!

  • Lifetime Qt Champion

    @Borzh Do you really use floating point data types for financial calculations?!
    Never do this, because floating point numbers are not exact.
    Use integer types and sore the values as Cent: 1$ = 100Cent



  • @SGaist said:

    QString currencyString = locale.toCurrencyString(1000.55);
    QString cleanedString = currencyString.remove(locale.currencySymbol()).trimmed();
    qDebug() << locale.toDouble(cleanedString);

    Thanks a lot, it works.



  • @jsulm said:

    @Borzh Do you really use floating point data types for financial calculations?!
    Never do this, because floating point numbers are not exact.
    Use integer types and sore the values as Cent: 1$ = 100Cent

    I know, but the precision is not important here.


Log in to reply