Left Padding a String with Zeros

Left padding a String with Zeros

If your string contains numbers only, you can make it an integer and then do padding:

String.format("%010d", Integer.parseInt(mystring));

If not I would like to know how it can be done.

How can I pad an integer with zeros on the left?

Use java.lang.String.format(String,Object...) like this:

String.format("%05d", yournumber);

for zero-padding with a length of 5. For hexadecimal output replace the d with an x as in "%05x".

The full formatting options are documented as part of java.util.Formatter.

How do I pad a string with zeroes?

To pad strings:

>>> n = '4'
>>> print(n.zfill(3))
004

To pad numbers:

>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n)) # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n)) # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n)) # python >= 2.7 + python3
004

String formatting documentation.

Angular2 or TypeScript Left padding a String with Zeros

You can create your own function for this. To format the number you will have to convert it to a string first.

function pad(num, size) {
let s = num+"";
while (s.length < size) s = "0" + s;
return s;
}

TypeScript

pad(num:number, size:number): string {
let s = num+"";
while (s.length < size) s = "0" + s;
return s;
}

Edit: There are a couple of better and more performant ways to do this.
See the discussion in this Answer: https://stackoverflow.com/a/9744576/1734678 (I recommend reading most of the submitted answers if you got time)

Update: ECMAScript 2017 now has support for string padding

str.padStart(targetLength [, padString])
str.padEnd(targetLength [, padString])

Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

EDIT: As mentioned by others, since Angular 4 you can use as below

{{ myNumber | number:'2.0' }}

What is the easiest way to pad a string with 0 to the left?

The fmt module documentation describes all the formatting options:

Fill / Alignment


The fill character is provided normally in conjunction with the
width parameter. This indicates that if the value being formatted is
smaller than width some extra characters will be printed around it.
The extra characters are specified by fill, and the alignment can be
one of the following options:

  • < - the argument is left-aligned in width columns
  • ^ - the argument is center-aligned in width columns
  • > - the argument is right-aligned in width columns

assert_eq!("00000110", format!("{:0>8}", "110"));
// |||
// ||+-- width
// |+--- align
// +---- fill

See also:

  • How can I 0-pad a number by a variable amount when formatting with std::fmt?
  • How do I print an integer in binary with leading zeros?
  • Hexadecimal formating with padded zeroes
  • Convert binary string to hex string with leading zeroes in Rust

How to pad a numeric string with zeros to the right in Python?

See Format Specification Mini-Language:

In [1]: '{:<08d}'.format(190)
Out[1]: '19000000'

In [2]: '{:>08d}'.format(190)
Out[2]: '00000190'


Related Topics



Leave a reply



Submit