In this example, we will show you how to add or attach a leading zero at the left of the number, or pad trailing zero at the right of the number in Flutter or Dart. This function is necessary when you are making any length of the number to the same width of strings with leading zeros.
String number = "345";
String number2 = "345345";
String numbera = number.padLeft(10, "0");
print(numbera); //Output: 0000000345
String numberb = number2.padLeft(10, "0");
print(numberb); //Output: 0000345345
Here, the number and number2 Strings have different lengths, but with padLeft(), you can make the same length of string with leading zeros. You can also replace zeros with any other characters to add at the leading of any string, for Example:
String number2 = "345345";
String numberc = number2.padLeft(10, "R");
print(numberc); //Output: RRRR345345
Here, we have attached other characters instead of zero.
String number2 = "345345";
String numberd = number2.padRight(10, "D");
print(numberd); //output: 345345DDDD
Here, we have attached "D' at the right side of the string to fulfill its length to 10.
In this way, you can add leading or trailing zeros on numbers in Dart or Flutter.
Please Wait...
No any Comments on this Article