Führende Nullen

Aus Thomas Wiki
Zur Navigation springen Zur Suche springen

Verschiedene Lösungen um Zahlen mit führenden Nullen auszugeben.


JavaScript[Bearbeiten]

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[Bearbeiten]

Gambas2[Bearbeiten]

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