toDigits method
Converts the integer to a string and left-pads it with zeroes until it reaches digits
length.
Useful for formatting numeric values such as dates, times, and identifiers where a fixed-width string is expected.
Example:
final padded = 7.toDigits(3); // '007'
final padded2 = 123.toDigits(5); // '00123'
Parameters:
digits
: The total number of digits the output string should have.
Returns:
A string version of this integer, padded with '0' on the left if necessary.
Throws:
- If
digits
is less than or equal to zero.
Notes:
- If the integer already has the same or more digits, it returns the number as a string.
Implementation
String toDigits(int digits) => toString().padLeft(digits, '0');