Führende Nullen
Verschiedene Lösungen um Zahlen mit führenden Nullen auszugeben.
JavaScript
function leading0 ( number , width ) {
if (isNaN(number) && isNaN(width)) {
return "--";
}
else {
s= "0";
for (i=1; i<width;i++) { s = s + "0" ; };
s = s + number;
return s.substr(s.length-width,width);
}
}
alert ( leading0 (123,5) ); // Ergibt: "00123"
alert ( leading0 (3,2) ); // Ergibt: "03"
Basic
Gambas2
FUNCTION Leading0(iNumber AS Integer, iWidth AS Integer) AS String
DIM result AS String
DIM i AS Integer
result = ""
FOR i = 1 TO iWidth
result = result & "0"
NEXT
RETURN Right(result & Str(iNumber), iWidth)
END FUNCTION
PUBLIC SUB Main()
PRINT leading0(127, 5)
END