Logo

Programming-Idioms

Assign to the string s the value of the integer i in 3 decimal digits. Pad with zeros if i < 100. Keep all digits if i1000.
Implementation
D

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another D implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "fmt"
s := fmt.Sprintf("%03d", i)
s := format('%.3d',[i]);
s = "%03d" % i
let s = format!("{:03}", i);
s = format(i, '03d')
$s = sprintf('%03d', $i);
String s = String.format("%03d", i);
my $s = sprintf '%03d', $i;
write (unit=s,fmt='(I0.3)') i
string s = string.Format("{0:000}",i);
(def s (format "%03d" i))
let s = i.toString();
if(i<100)
  s = ('00'+i).slice(-3);
var s = i.toString().padLeft(3,'0');