I want to format an integer into a padded string the int range will be 0 to 55
0 - 00
1 - 01
2 - 02
String strTemp = String.Format({"0#"},counter); -- this does not seem to be correct.
Would appreciate any help here - thanks in advance
I want to format an integer into a padded string the int range will be 0 to 55
0 - 00
1 - 01
2 - 02
String strTemp = String.Format({"0#"},counter); -- this does not seem to be correct.
Would appreciate any help here - thanks in advance
Pick your favorite.
int value = 1; //42;
char buff[3];
char hbuff[3];
String crap;
void setup() {
Serial.begin(115200);
sprintf(buff, "%02d", value);
Serial.println(buff);
hbuff[0] = (value / 10 ) + '0';
hbuff[1] = (value % 10 ) + '0';
Serial.println(hbuff);
crap = String(value);
if (crap.length() < 2) {
crap = "0" + crap;
}
Serial.println(crap);
}
void loop() {}
01
01
01
@dolittle Please post a link to the page describing that String.Format() function, I have not seen that before and it sounds useful.
You could try
String strTemp = (counter < 10 ? "0" : "") + String(counter);
another option
char buffer[10];
void setup() {
Serial.begin(115200); Serial.println();
for (int value = 0; value < 12; value++) {
snprintf(buffer , sizeof buffer, "%02d", value);
Serial.print(value);
Serial.print(F("\t-> ["));
Serial.print(buffer);
Serial.println(F("]"));
}
}
void loop() {}
Serial Monitor (@ 115200 bauds) will show
0 -> [00]
1 -> [01]
2 -> [02]
3 -> [03]
4 -> [04]
5 -> [05]
6 -> [06]
7 -> [07]
8 -> [08]
9 -> [09]
10 -> [10]
11 -> [11]
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.