Add leading zeroes to number in Dart

Using NumberFormat from import 'package:intl/intl.dart';

NumberFormat formatter = new NumberFormat("00000000");

// The following outputs your expected output
debugPrint("123 should be ${formatter.format(123)}");
debugPrint("1243 should be ${formatter.format(1243)}");
debugPrint("123456 should be ${formatter.format(123456)}");
debugPrint("12345678 should be ${formatter.format(12345678)}");

Result:

123 should be 00000123
1243 should be 00001243
123456 should be 00123456
12345678 should be 12345678

DartPad

void main() {
 print(123.toString().padLeft(10, '0'));
}

Tags:

Dart