Hi!
Isn’t it annoying that the “hour()”, “minute()”, “second()”, … functions output unorganised integers?
(For example, if the time is 21:05:09, than the second() function would return “9” instead of “09”)
Here is a simple program to fix it!
Here is the code:
void setup() {
}
void draw() {
println("The better way: " + loadZeros(hour(),2) + ":" + loadZeros(minute(),2) + ":" + loadZeros(second(),2));
println("The normal way: " + hour()+":"+minute()+":"+second());
println("");
}
String loadZeros(int number, int len) {
if((number+"").length() < len) {
String prefix = "";
for(int i = 0; i < len-(number+"").length(); i++) {
prefix += "0";
}
return(prefix + number);
} else {
return(number+"");
}
}
With this you can also turn any intiger into intigers with length. (input loadZeros(131,5), output=“00131” (in String form).
Feel free to use it in any of your projects!