How to validate the price in Qt?

Asked

Viewed 196 times

0

I would like to know how to validate the price in Qt, as follows:

Accepted examples: 10,00 / 100,00 / 1.000,00 = true

My code, (but it’s validating true: 10 / 100)

 bool ok;
 QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
 QLocale brazil; // Constructs a default QLocale
 brazil.toDouble(ui->price->text(), &ok);
 qDebug() <<  ok;
  • accepted example: 1.00,00 ?

  • @Math: heh, I missed. I missed a zero. Ae Math knows how to validate? I’ll be very grateful for the help. :)

  • I’d love to help, but I really don’t know what it is qt... soh one more thing that left me in doubt on your question: mas ta validando true: 10 / 100 / 1000.00 means that the q has comma it ta returning false and only the examples without comma return true?

  • Look, I just tested here with brazil.toDouble("1000.00", &ok);, for example, and it worked (I mean, returned false as expected).

  • However, for values like 10 and 100 (no point in the place of the comma) I believe that the validation will always be correct, because they are valid numbers in the locale of Brazil.

  • You should explain your question better, I tried to help though, still got confused.

  • ah, now I understand, to return true has to have the . separating the decimals and the , if the entire part passes 999, this Voce expects @user628298 ?

  • @Math: yes. True=1,000.00 false=1000.00

  • it seems that Qlocale does not have this format check functionality you want, better search for other means of checking, for example regex.

  • @user628298 But, your code ALREADY does that. At least for the examples you just commented on.

  • @Luiz Vieira: Yes back false. I added wrong :)

  • Ok, so your question really isn’t clear. : ) What’s the problem? Wouldn’t you NOT want them to validate values without the comma? If that’s the case, the @pepper_chico suggestion looks ideal: use a regular expression.

  • 1

    @Luizvieira to me seems that he does not want it to pass if it has no comma. This seems clear, and this Qlocale does not offer.

  • @pepper_chico It seems that way. But the question does not say that clearly.

  • @Luiz Vieira: better a regex or add one código if(ui->preco->text().length() <= 2){ qDebug() << text.sprintf("%6.2f", ui->preco->text().toDouble(); } ? código

  • @Guilhermebernal just posted an answer that should help you. :)

  • Thank you, all of you :)

Show 12 more comments

1 answer

3


If you want to validate precisely a number in the format of a price (exactly two decimal places, without being in exponential format, comma separator, thousands point separator), you should use something more precise than trying a conversion to double. Do it with a regex:

QRegularExpression priceRe("^[0-9]{1,3}(\\.[0-9]{3})*,[0-9]{2}$");
QString price = "1.234,53";
if (priceRe.match(price).hasMatch())
    qDebug() << "matched";

regexplained

  • Blz. Thank you very much.

Browser other questions tagged

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