Padding a Number with Zeros

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.

Padding a number with zeros

puts 1.to_s.rjust(3, "0")
#=> 001
puts 10.to_s.rjust(3, "0")
#=> 010
puts 100.to_s.rjust(3, "0")
#=> 100

The above code would convert your user.id into a string, then String.rjust() method would consider its length and prefix appropriate number of zeros.

How to pad a number with zeros to a specific length in tcl?

It's really simple, actually:

format %-08s $input_num

A number is also a string, so it can be left-justified like a string.

Documentation: format

How can I pad a value with leading zeros?

Since ECMAScript 2017 we have padStart:

const padded = (.1 + "").padStart(6, "0");
console.log(`-${padded}`);

How can I pad a string with a variable number of leading zeros?

You will need to utilize a couple Expressions to handle this.

  1. Declare a String variable to store the input (e.g. named InputVar). You will need to continue using the concat() Expression here with the input value to add the maximum possible zeros required for each item (e.g. 4 for BoxNo and 5 for ItemID).

    This is obviously inefficient, but there's no better way to insert a dynamic number of characters in Flow, to my knowledge.

  2. Declare a second Integer variable to determine the length (e.g. named InputVarLength), with the following custom Expression as the value:

     length(variables('InputVar'))
  3. Finally, declare a third variable that will calculate a substring result (e.g. named InputVarResult). Use this custom substring Expression as the value:

     substring(variables('InputVar'),sub(variables('InputVarLength'),5),5)

For the ItemID or other results, you'd replace the 5s in the substring Expression with the appropriate startIndex and length to return the size you would like. For reference, the substring format is:

substring(text, startIndex, length)

Which includes the String value you want to find a substring of, the position within that string that you want to start from, and how many characters from that starting position you want to include in your substring result.

The nested subtract format is:

sub(minuend, subtrahend)

Which includes the Integer value to be subtracted from (minuend) and the Integer value that you want to subtract (subtrahend). E.g. sub(10, 2) would return an Integer value of 8.

How to zero pad a sequence of integers in bash so that all have the same width?

In your specific case though it's probably easiest to use the -f flag to seq to get it to format the numbers as it outputs the list. For example:

for i in $(seq -f "%05g" 10 15)
do
echo $i
done

will produce the following output:

00010
00011
00012
00013
00014
00015

More generally, bash has printf as a built-in so you can pad output with zeroes as follows:

$ i=99
$ printf "%05d\n" $i
00099

You can use the -v flag to store the output in another variable:

$ i=99
$ printf -v j "%05d" $i
$ echo $j
00099

Notice that printf supports a slightly different format to seq so you need to use %05d instead of %05g.

Left zero padded format numbers

Try %04d instead:

  • 0 indicates what you're using to pad;
  • 4 is the width of the number (you had 5 before);
  • d represents an integer (incl. byte, short, int, long, bigint).
String.format("%04d", Integer.parseInt(something));


Related Topics



Leave a reply



Submit