Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an int to a QString with zero padding (leading zeroes)

Tags:

c++

qt

qstring

I want to "stringify" a number and add zero-padding, like how printf("%05d") would add leading zeros if the number is less than 5 digits.

like image 663
elcuco Avatar asked Apr 11 '10 19:04

elcuco


1 Answers

Use this:

QString number = QStringLiteral("%1").arg(yourNumber, 5, 10, QLatin1Char('0')); 

5 here corresponds to 5 in printf("%05d"). 10 is the radix, you can put 16 to print the number in hex.

like image 72
chalup Avatar answered Oct 04 '22 22:10

chalup