# 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

# Japt, 13 8 bytes

Takes first input (x) as a string.

®©ùTV}'-


Try it

Saved a massive 5 bytes thanks to ETHproductions.

## Explanation

Implicit input of string U=x and integer V=y.

® }'- splits U to an array on the minus symbol, maps over it and rejoins it to a string with a minus symbol.

© is logical AND (&&) so if the current element is truthy (a non-empty string) then pad left (ù) with 0 (T) to length V.

• Nice one! You can save quite a bit by simply mapping around -: ethproductions.github.io/japt/… – ETHproductions Dec 1 '17 at 19:40
• @ETHproductions: Great call. Thanks. Been so long since I've done so, I'd completely forgotten you can split, map and rejoin a string all in one method! – Shaggy Dec 1 '17 at 20:59
• Yes, I suppose that functionality should be moved to q, which would then be q-_©ùTV to save 1 byte :-) – ETHproductions Dec 1 '17 at 23:51
• @ETHproductions, if I'm understanding that right, you're suggesting that if a function is passed as a second argument of S.q() (giving us S.q(s,f)) then S would be split on s, run through f and rejoined with s? I like it! :) – Shaggy Dec 2 '17 at 0:05
• Yeah, did I talk to Oliver and not you about doing that (if passed a function, do the normal functionality, run through the function, and undo the first change; N.s, S/A.y, N.ì do this already) with a bunch of methods? I had a conversation with someone, I just can't remember who now :s – ETHproductions Dec 2 '17 at 0:31

# Pyth, 12 bytes

%"%0*d",+E>0


Try it here!

# Python 2, 29 bytes

lambda x,y:x.zfill(y+(x<'.'))


Try it online!

Just str.zfill comes so close.

# 05AB1E, 11 10 bytes

Input given as amount_of_digits, number

ÎIÄg-×ì'-†


Try it online!

Explanation

Î            # push 0 and first input
IÄ          # push the absolute value of the second input
g         # length
-        # subtract, (input1-len(abs(input2))
×       # repeat the zero that many times
ì      # prepend to the second input
'-†   # move any "-" to the front

• Seems like this is the winning answer so far, if not bettered tomorrow i'll accept it. – Brian H. Dec 4 '17 at 10:16
• you've been outgolfed :( – Brian H. Dec 5 '17 at 9:32
• @BrianH. Indeed I have :) – Emigna Dec 5 '17 at 9:47

# Python, 29 bytes

Take input as f(x,y). Using Python's % operator.

lambda x,y:'%0*d'%(y+(x<0),x)


Try it online!

# C#, 56 bytes

Try it Online!

a=>b=>(a<0?"-":"")+((a<0?-a:a)+"").PadLeft(b<0?0:b,'0')


# Java (OpenJDK 8), 47 bytes

x->y->"".format("%0"+((y<1?1:y)-(x>>31))+"d",x)


Try it online!

At first I thought, easy, 30 chars max (which is quite short when manipulating strings in Java). Then the exceptions happened.

# JavaScript (ES6), 42

Recursive, parameters in reverse order, first y then x. And Currying

y=>r=x=>x<0?'-'+r(-x):${x}.padStart(y,0)  Test var F= y=>r=x=>x<0?'-'+r(-x):${x}.padStart(y,0)

;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"
.split(\n).map(r => r.match(/[-\d]+/g))
.forEach(([x,y,k])=>{
o = F(y)(x)
ok = o == k
console.log(ok?'OK':'KO',x,y,'->', o)
})

• While impressive, I feel this answer skirts the rules slightly by defining the function f(y)(x) instead of f(x,y). – styletron Dec 1 '17 at 14:46
• Reading up on the "currying" rules, I wanted to add that my objection was more with the reversed params and not with the currying itself. – styletron Dec 1 '17 at 14:58
• @styletron the order of the parameters is not specified in the challenge. So I can take advantage of this – edc65 Dec 1 '17 at 15:04
• Dang, y=>r=x=>x<0?'-'+r(-x):(x+='')[y-1]?x:r(0+x) comes so close... – ETHproductions Dec 2 '17 at 0:01
• i have no issues with people reversing the input order. – Brian H. Dec 4 '17 at 10:13

# Python 3.6, 28 37 bytes

lambda x,y:f'{x:0{(y,0)[y<0]+(x<0)}}'


Try it online! (Test case from Colera Su's answer)

Taking advantage of the new way to format strings in python 3.6

+9 bytes to handle y<0

• Looks like this fails when y is negative: tio.run/##K6gsycjPM/… – Shaggy Dec 2 '17 at 11:38
• Indeed. So i'm up with 37 bytes. – Lescurel Dec 2 '17 at 14:22

# bash, 27, 25 bytes

-2 bytes thanks to Bruce Forte

printf %0$[$2+($1<0)]d$1


try it online

• You can save two bytes by inlining the padding length. Also another interesting one (28 bytes): printf %\ 0$[1+$1]d $2|xargs. – ბიმო Dec 2 '17 at 17:55 • maybe you mean printf %\ 0$[1+$2]d$1|xargs, i didn't remember this format for signed numbers also xargs trick to remove leading space – Nahuel Fouilleul Dec 2 '17 at 19:28

# Husk, 12 bytes

Ö±Ωo≥⁰#±:'0s


Try it online!

## Explanation

Ö±Ωo≥⁰#±:'0s  Inputs are y=4 and x=-20
s  Convert x to string: "-20"
:'0   Prepend '0'
Ω           until
#±      the number of digits
o≥⁰        is at least y: "00-20"
Ö±            Sort by is-digit: "-0020"
Print implicitly.


# R, 56 48 bytes

function(x,y)sprintf(paste0("%0",y+(x<0),"d"),x)


Try it online!

-8 bytes thanks to djhurio

### Explanation

• sprintf("%0zd",x) returns x as a string padded with zeros to be of length z
• paste0("%0",y+(x<0),"d") constructs the string "%0zd", where z is y, plus 1 if x is less than zero
• If z is less than the number of digits in x, x is printed as a string as is
• 48 bytes function(x,y)sprintf(paste0("%0",y+(x<0),"d"),x) – djhurio Dec 2 '17 at 14:05
• @djhurio brilliant! I think that would warrant another answer rather than an edit of mine, what do you say? – duckmayr Dec 2 '17 at 14:40
• You can make it as an edit. This solution is not so much different, just using different function. – djhurio Dec 2 '17 at 18:14

## Alice, 23 bytes

/oRe./'+Ao
\I*tI&0-R$@/  Try it online! Input should be linefeed-separated with the number on the first line and the width on the second. ### Explanation /... \...@/  This is the usual framework for linear programs in Ordinal mode. The only catch in this case is this bit: .../... ...&...  This causes the IP to enter Cardinal mode vertically and execute just the & in Cardinal mode before resuming in Ordinal mode. Unfolding the zigzag control flow then gives: IRt.&'-A$o*eI/&/0+Ro@

I    Read the first line of input (the value) as a string.
R    Reverse the string.
t.   Split off the last character and duplicate it.
&    Fold the next command over this string. This doesn't really do anything,
because the string contains only one character (so folding the next
command is identical to executing it normally).
'-   Push "-".
A    Set intersection. Gives "-" for negative inputs and "" otherwise.
$*0 ((\d)*),(?<-2>-0+|0)*(0*)$3$1  Try it online! Input should be comma-separated with the number first and the width second. # PHP, 45 bytes printf("%0".($argv+(0>$n=$argv)).d,$n);  or [,$n,$e]=$argv;printf("%0".($e+(0>$n)).d,$n); # requires PHP 7.1 or later  Run with -nr or try them online. • Getting an error when executing the code in that link. – Shaggy Dec 2 '17 at 11:43 • @Shaggy The second version requires PHP 7.1 – Titus Dec 2 '17 at 12:41 • I went on and on about this, and I arrived to exactly this answer. I believe this is the optimal version – Ismael Miguel Dec 2 '17 at 16:38 # Mathematica, 118 bytes (j=ToString;If[#2<=0,j@#,If[(z=IntegerLength@#)>=#2,t=z,t=#2];s=j/@PadLeft[IntegerDigits@#,t];If[#>=0,""<>s,"-"<>s]])&  # Mathematica, 63 62 bytes If[#<0,"-",""]<>IntegerString[#,10,Max[#2,IntegerLength@#,1]]&  Try it online! • Welcome to PPCG! I think this isn't quite doing what you want. You probably meant IntegerLength instead of IntegerDigits. You can save a byte by using IntegerLength@# instead of IntegerLength[#] though. – Martin Ender Dec 1 '17 at 17:47 • Thank you! I was copying the code from another computer by hand where I was testing it and I indeed mistyped IntegerDigits for IntegerLength. It should work now. I have also added a TIO link with all the test cases in the challenge description (+1) showing that it works as expected. Thank you also for the suggestion for saving an extra byte! I don't know how I missed it before. :) – MatjazGo Dec 4 '17 at 9:52 # Excel, 29 bytes Using Excel's TEXT functionality ("Converts a value to text in a specific number format"). x in A1, y in B1 =TEXT(A1,REPT("0",MAX(1,B1)))  • You can drop the ))) for -3 Bytes by converting to Google Sheets – Taylor Scott Dec 18 '17 at 17:46 # Octave, 44 bytes @(x,y)fprintf(['%0',num2str(y+(x<0)),'d'],x)  Try it online! • For 31 bytes – Tom Carpenter Dec 1 '17 at 17:24 • If leading + signs are allowed for positive numbers (waiting to hear from op if ok), this works for 28. – Tom Carpenter Dec 1 '17 at 17:25 # Haskell, 54 bytes x#y|s<-show$abs$x=['-'|x<0]++('0'<$[length s+1..y])++s


Try it online!

# Ruby, 31 28 bytes

Thanks Carl for saving 3 bytes using interpolation.

->x,y{"%0#{y+(x<0?1:0)}d"%x}


Try it online!

• 28: ->x,y{"%0#{y+(x<0?1:0)}d"%x} – Carl Dec 1 '17 at 17:44

# Japt, 14 12 bytes

Saved 2 bytes thanks to @ETHproductions

s r"%d+"_ù0V


Try it online

• It's a bit cheaper to keep the minus sign in and just mess with the digits: Test it online – ETHproductions Dec 1 '17 at 17:03
• @ETHproductions: Or take x as a string for 10 bytes. – Shaggy Dec 1 '17 at 17:23
• @ETHproductions thanks fellas. I'll update it when I get back to my desk. – Oliver Dec 1 '17 at 18:55
• @Shaggy Looks like you posted your own answer, so I'll use ETHproduction's trick. Thanks though. – Oliver Dec 1 '17 at 20:29
• Oliver, that 10-byter is just @ETHproduction's 12-byte solution upgraded to Japt 2.0a0 with U as a string allowing us to golf off the first 2 characters. – Shaggy Dec 1 '17 at 21:08

# PowerShell, 25 40 bytes

param($a,$b)$a|% *g$("D$b"*($b|% *o 0))


Try it online!

This calls .ToString() on the number with a generated format string, but multiplies it by -1, 0, or 1 based on whether $b (y) is negative, 0, or positive respectively; this is to handle negative y values which format strings don't by themselves. This seems to require wrapping negative numbers in a substatement () for it to work which is just a quirk of the invocation when using literals; if passed variables of type integer, it would not need that. • It looks like both of these fail when y is negative. – Shaggy Dec 2 '17 at 11:45 • @Shaggy ugh good catch. Removed second solution altogether and fixed up the first, thanks! – briantist Dec 2 '17 at 16:02 • Ouch, 15 bytes! Sorry! – Shaggy Dec 2 '17 at 18:54 • @Shaggy heh, one of these days I'll actually write the PowerShell-based golfing language I've been thinking about. This actually pushed me to research a bit more and get closer to starting it, so thanks for that ;) – briantist Dec 2 '17 at 18:56 # C# 6.0, 35 bytes (x,y)=>(x.ToString($"D{y<0?0:y}"));


### Alternative solution (51 bytes)

(x,y)=>(x.ToString(string.Format("D{0}",y<0?0:y)));


# Clean, 9086 83 bytes

import StdEnv
@x y=if(y<0)"-"""+++{if(c>'-')c'0'\\c<-rjustify x[k\\k<-:toString y]}


Try it online!

# C (gcc), 45 bytes

f(x,y){printf("%s%0*i","-"+(x>=0),y,abs(x));}


Try it online!

## Explanation

printf formats three arguments:

%s      ->    "-"+(x>=0)
%0*i    ->    y
->    abs(x)


%s formats the string "-"+(x>=0). "-" is really just an address, something like 41961441. In memory, this looks something like this:

MEMORY ADDRESS | 41961441  41961442 ...
VALUE          | 45 ('-')  0 (0x00) ...


When formatted into a string, C takes the address (say 41961441) and keeps on acquiring characters until a null byte (0x00) is met. When x is less than zero, the value "-"+(x>=0) has that of the original address (41961441). Otherwise, x>=0 is 1, so the expression becomes "-"+1, which points the null byte after "-", which prints nothing.

%0*i prints an integer padded with a specified number of 0s. y denotes this number. We pad abs(x) to avoid the negative in some arguments.

## Perl, 25 + -n flag = 26 bytes

printf"%0*d",<>+($_<0),$_


Try it online!

# Perl 5, 22 + 1 (-n) = 23 bytes

printf"%0*d",<>+/-/,\$_


Try it online

• Loops infinitely on 10,-1 – Zaid Dec 1 '17 at 13:31
• fixed +5 bytes – Nahuel Fouilleul Dec 1 '17 at 14:07
• found another solution -5 bytes – Nahuel Fouilleul Dec 1 '17 at 14:25
• Sweet. I'm working on one as well... stay tuned – Zaid Dec 1 '17 at 14:28
• I managed it in 26 bytes – Zaid Dec 1 '17 at 14:45