0

I'm using Qt 5.4. I need to format a QString representing a number and make sure that it's always with 3 digits, adding trailing zeros.

Let me give an example:

  • If the string is "1" I need "100".

  • If the string is "13" I need "130".

  • If the string is "472" no changes.

Is there a way to do this without checking the length of the string?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Alfredo
  • 5
  • 4

1 Answers1

0

If you rellay don't want to use the lenght.... use the maths!

my_str = Qstring::number(my_str.toInt()*pow(10,2-log10(my_str.toInt())))

or:

my_str = my_str.append('0',2-log10(my_str.toInt())) 

but it would be much simpler/quick to do this:

my_str = my_str.append('0',3-my_str.length())
bibi
  • 3,671
  • 5
  • 34
  • 50