Führende Nullen: Unterschied zwischen den Versionen

Aus Thomas Wiki
Zur Navigation springen Zur Suche springen
KKeine Bearbeitungszusammenfassung
 
Zeile 22: Zeile 22:
== Basic ==
== 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


[[Kategorie:Basic]]
[[Kategorie:Basic]]
[[Kategorie:Gambas2]]
[[Kategorie:JavaScript]]
[[Kategorie:JavaScript]]

Aktuelle Version vom 7. Februar 2012, 19:48 Uhr

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