# The challenge

Given two integers as input (x and y), output x as a string with as many leading zeroes necessary for it to be y characters long without the sign.

## Rules

• If x has more than y digits, output x as string without modification.

• Output as integer is not accepted, even if there are no leading zeroes.

• When x is negative, keep the - as is and operate on the absolute value.

• Negative y should be treated as 0, meaning you output x as is (but as string)

### Examples:

IN: (1,1)   OUT: "1"
IN: (1,2)   OUT: "01"
IN: (10,1)  OUT: "10"
IN: (-7,3)  OUT: "-007"
IN: (-1,1)  OUT: "-1"
IN: (10,-1) OUT: "10"
IN: (0,0)   OUT: "0"
IN: (0,1)   OUT: "0"
IN: (0,4)   OUT: "0000"


Shortest code in bytes wins, standard loopholes apply.

• – Brian H. Dec 1 '17 at 9:49
• Can I take x as string? – LiefdeWen Dec 1 '17 at 9:56
• what does (-1,1) give? – Adám Dec 1 '17 at 9:59
• @Adám added it to the examples. – Brian H. Dec 1 '17 at 10:42
• Is a leading + sign acceptable for positive numbers? – Tom Carpenter Dec 1 '17 at 17:21

# Perl 6,  39 31  30 bytes

->\a,\b{'-'x(0>a)~a.abs.fmt("\%0{b}d")}


Test it

->\a,\b{a.fmt("\%0{b+(0>a)}d")}


Test it

{$^a.fmt: '%0'~$^b+(0>$a)~'d'}  Test it ## Expanded: { # bare block lambda with placeholder parameters ｢$a｣ and ｢$b｣$^a         # declare and use first parameter
.fmt:       # format it

'%0'        # '%05d'
~
$^b # make it ｢$b｣ digits wide
+ (0>$a) # add 1 if ｢$a｣ is negative
~
'd'
}


# PHP, 49 bytes

echo str_pad($argv,$argv,'0',STR_PAD_LEFT);

• Instead of STR_PAD_LEFT, you can use 0. Also, this fails for -7,3 (returns 0-7 instead of -007). – Ismael Miguel Dec 2 '17 at 16:27

# Husk, 18 bytes

Likely not the right approach, this is feels really long for Husk, but there wasn't one already.

F+Ṁ+R'0-Ld⁰¹↕='-s⁰


Try it online!

# Pip, 13 bytes

aR+XD0Xb-#_._


Takes the number and minimum number of digits as command-line arguments. Try it online!

### Explanation

               a, b are cmdline args; XD is regex \d (implicit)
aR             In the number a, replace
+XD           a run of digits (regex \d+)
with this callback function:
0X          0 string-multiplied by
b-#_      minimum digit count minus length of match
._    to which concatenate the match itself
Autoprint the result of the replacement


Since string-multiplication gives an empty string when the right operand is negative, this works correctly for all test cases.

# Stacked, 30 bytes

[@x'@:'!'\d+'['0'x pad]1/repl]


Try it online!

Highlighted: ## Explanation

[@x'@:'!'\d+'['0'x pad]1/repl]
[                            ]   anonymous function, take two args: (y x)
'@:'!                         convert to string*
'\d+'[        ]1/repl    replace all runs of digits with...


## Stringification

Usually, one would use tostr to convert something to a string; however, due to the way string formatting works...

'@:'!
!   format the string
'@:'    apply the : function t the top of the stack
this creates a sub-stack, and : duplicates.
the formatting obtains the top value, which is just the original TOS,
and casts it to a string


The reason we use '@:'! instead of tostr is to avoid the space between @x and tostr.

0;$L<⁹$$¿ ṣ”-Ç€0¦j”-  Try it online! Take first input x as a string. Apparently there is no existing Jelly answer... L<¥ doesn't seem to work. # SNOBOL4 (CSNOBOL4), 95 89 bytes  DEFINE('F(X,B)') F L =GT(B) B N =LT(X) 1 F =DUPL('-',N) LPAD(-1 ^ N * X,L,0) :(RETURN)  Try it online! defining a function is equivalent to a full program in bytes, so I figured I'd do this instead. # Tcl, 46 bytes proc Z n\ p {format %0[expr$n<0?$p+1:$p]d \$n}


Try it online!

## Swift, 37 bytes (65 inside a function):

String(format:"%0\(y+(x<0 ?1:0))d",x)


Inside a function:

func c(x:Int,y:Int){print(String(format:"%0\(y+(x<0 ?1:0))d",x))}


# Java, 218 bytes

 import org.apache.commons.lang3.StringUtils;interface B{static void main(String z[]){int x=Integer.parseInt(z);if(x>z.length()){System.out.printf(StringUtils.leftPad(z,x,"0"));}else{System.out.printf(z);}}}


Ungolfed:

import org.apache.commons.lang3.StringUtils;

interface B {
static void main(String z[]) {
int x = Integer.parseInt(z);
if (x > z.length()) {
} else {
System.out.printf(z);
}
}
}


# Sinclair ZX81/Timex TS1000/1500 BASIC, ~163 tokenized BASIC bytes

 1 DIM A(SGN PI,VAL "2")
2 LET A(SGN PI,SGN PI)=NOT PI
3 LET A(SGN PI,VAL "2")=INT PI
4 IF A(SGN PI,SGN PI)<NOT PI THEN PRINT "-";
5 IF A(SGN PI,VAL "2")=SGN PI THEN GOTO INT PI*PI
6 FOR I=VAL "2" TO A(SGN PI,VAL "2")
7 PRINT NOT PI;
8 NEXT I
9 PRINT ABS A(SGN PI,SGN PI)


Explanations: ZX81 BASIC is quite costly when storing numbers as each number is stored as a 5 byte (I think) floating point number even if you store an integer value like 1. Even line numbers cost 5 bytes per line, then add in the NEW LINE character at the end of each line, and the lowest byte count for a BASIC line in Sinclair ZX81 BASIC is 7 (for instance, 1 PRINT). SGN PI is 1, and costs fewer bytes than 1 on this basis, NOT PI is 0 and costs two bytes etc.

• Line 1 sets up a numeric array 1, 2; arrays are not zero-indexed in this case.
• Line 2 sets A[1, 1] to zero.
• Line 3 sets A[1, 2] to 3, so the test case is (1,3).
• Line 4 tests to see if the number to be printed is negative; if so, a minus sign is displayed.
• Line 5 checks if the padding is set to 1, if so we don't need to print any leading zeros.
• Line 6 then loops through from 2 to the value of our padding (in this case, 3).
• Line 7 prints the leading zeros.
• Line 8 ends the loop.
• Line 9 prints the positive value of our A[1,1], which in this case is zero.

# Jelly, 18 bytes

R¬+Ṛ}AṚṾ€”-x⁴<0¤¤;


Try it online!

Takes y and x (in that order) as integers.

Explanation

Example input 5 and -123.

R¬+Ṛ}AṚṾ€”-x⁴<0¤¤;  Dyadic link
R¬                  Not the range of y. This gives a list of y zeroes. [0,0,0,0,0]
Ṛ}               Reverse the digits of x. [-3,-2,-1]
A              Absolute value. [3,2,1,0,0].
Ṛ             Reverse. [0,0,1,2,3]
Ṿ€           Make each digit a string. '00123'
”-         The character '-'.
⁴<0     x<0? 0 or 1. (1 in this example)
x        Repeat '-' 0 or 1 times
¤¤   Closes ”-x⁴<0 as a nilad
;  Prepend '-' to '00123'


The absolute difference atom ạ would work here over + making A unnecessary except for the case where y is zero or negative where it doesn't work.

# Excel VBA, 33 Bytes

Anonymous VBE immediate window function that takes input as n from [A1] and length from [B1] and outputs the appropriately formatted string to the VBE immediate window

?Format([A1],[Rept(0,Max(1,B1))])