Here's a LuaLaTeX-based solution, which uses Lua's built-in string.format function and sets up a user macro called \padnum . Note that the \padnum macro is fully expandable.

The default number of padding digits is 4; this can be overridden by supplying the number of padding digits as the optional argument of \padnum . The macro handles negative numbers without any issues, and it can be executed in both text mode and math mode.