Convert locale-specific QString to Decimal
-
Hi all,
I googled a bit but it appears this question wasn't raised before... (or I need to learn for better search requests).
My intention is to store a floating point number entered by a user in python variable with typeDecimal
. And user is allowed to do some nice formatting, like "10 400,3" for example, to allow easy copy-pasting.Previously I did it for
float
and it worked fine this way:
value = QLocale().toDouble(text)[0]
wheretext
is a string received fromQLineEdit
that was conveniently validated withQDoubleValidator
.Now I need basically to do the same but I want to have
Decimal
type ofvalue
.
If I do type a simple type castingDecimal(QLocale().toDouble("10 400,3")[0])
then I have a predictably ugly result that is precise in some sence but wrong in reality and not useful:Decimal('10400.29999999999927240423858165740966796875')
.I tried to cast to
str
firstDecimal(str(QLocale().toDouble("10 400,3")[0]))
and it gives meDecimal('10400.3')
. But I have no control howstr()
convertsfloat
to string and a bit afraid that at some moment I may have something strange there. Probably I have not enough knowledge about how python does this conversion under the hood.So, the question is - what would be a nice and safe way of locale-specific
QString
converstion toDecimal
?Or maybe there is a way to "nomalize" initial string? I.e. to trasform "10 400,3" into "10400.3" that will be suitable for usage in Decimal constructor? (Something like Python's locale.delocalize() method, but I can't use it as I don't want to change locale on the fly...)
-
Thanks @SGaist, yes I think I can.
I just expected Qt to have something like aforementioneddelocalize()
method but it seems it hasn't.
So I'll go with replacements as there are no better ideas. -
Hi all,
I googled a bit but it appears this question wasn't raised before... (or I need to learn for better search requests).
My intention is to store a floating point number entered by a user in python variable with typeDecimal
. And user is allowed to do some nice formatting, like "10 400,3" for example, to allow easy copy-pasting.Previously I did it for
float
and it worked fine this way:
value = QLocale().toDouble(text)[0]
wheretext
is a string received fromQLineEdit
that was conveniently validated withQDoubleValidator
.Now I need basically to do the same but I want to have
Decimal
type ofvalue
.
If I do type a simple type castingDecimal(QLocale().toDouble("10 400,3")[0])
then I have a predictably ugly result that is precise in some sence but wrong in reality and not useful:Decimal('10400.29999999999927240423858165740966796875')
.I tried to cast to
str
firstDecimal(str(QLocale().toDouble("10 400,3")[0]))
and it gives meDecimal('10400.3')
. But I have no control howstr()
convertsfloat
to string and a bit afraid that at some moment I may have something strange there. Probably I have not enough knowledge about how python does this conversion under the hood.So, the question is - what would be a nice and safe way of locale-specific
QString
converstion toDecimal
?Or maybe there is a way to "nomalize" initial string? I.e. to trasform "10 400,3" into "10400.3" that will be suitable for usage in Decimal constructor? (Something like Python's locale.delocalize() method, but I can't use it as I don't want to change locale on the fly...)
I got one idea here... not ideal but probably it might work for my task.
Formatted numbers are different from C-style numbers by decimal separator symbol (that may be usually dot or comma) and by group separator.
So theoretically this should work:
text.replace(QLocale().groupSeparator(), '').replace(QLocale().decimalPoint(), '.')
.
But in reality it doesn't :( At least I tried with my locale and got a problem becauseQLocale.groupSeparator()
returns0xA0
that is unbreakable space. This is correct, but the problem is - my number contains normal space0x20
and simple code above doesn't work...
So the best current idea is:
Decimal(text.replace(' ', '').replace(QLocale().groupSeparator(), '').replace(QLocale().decimalPoint(), '.'))
but I'm open to have a better one :) -
Hi,
You can use a regular expression to replace all whitespaces with nothing.
-
Thanks @SGaist, yes I think I can.
I just expected Qt to have something like aforementioneddelocalize()
method but it seems it hasn't.
So I'll go with replacements as there are no better ideas. -
What is usually done is separation of presentation and storage. Typically, when using a database, you store the numbers in a "neutral" form and your application will show them with whatever representation makes sense.
It might also be something you can apply here. Show the content as "locale aware" and store the value as its numerical representation.
I can't comment more not knowing which widget(s) you are using.