/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.
$o If it's "-", print it, otherwise it must have been a digit which we
leave on the stack.
* Join the digit back onto the number. If the number was negative, this
joins the (absolute value of the) number to an implicit empty string,
doing nothing.
e Push an empty string.
I Read the width W.
/&/ Iterate the next command W times.
0 Append a zero. So we get a string of W zeros on top of the absolute
value of the input number.
+ Superimpose. This takes the character-wise maximum of both strings
and appends extraneous characters from the longer string. Since the
string of zeros can never be larger than the digits in the input,
the input itself will be uneffected, but extraneous zeros are appended,
padding the string to the required length.
R Reverse the result.
o Print it.
@ Terminate the program.
Here are two alternatives, also at 23 bytes, which use Cardinal H
(abs) to get rid of the -
:
/R.I&0-RoH
\Ie#\'+Ao\@/
/R.H#/.+Xo
\Ie\I&0QRo@/
In principle, these are a command shorter, but the &
doesn't fit into a position where there's a 1-character string on the stack, so we need to skip it with a #
.
(-1,1)
give? \$\endgroup\$+
sign acceptable for positive numbers? \$\endgroup\$