1

I would like to create a number with N digits.

Example;

myVar=543 and I would like it with 6 digits so myVar will be 000543

myVar=44345 will be 044345
myVar=1 will be 0000001
...

I do that with Batch, so just with Windows Batch commands.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • Is that https://stackoverflow.com/questions/13398545/string-processing-in-windows-batch-files-how-to-pad-value-with-leading-zeros working for you? – Nghia Do Mar 14 '18 at 13:44

2 Answers2

2

add 6 zeros at the front of the number and then cut the last 6 characters:

set myVar=51
echo 1: %myVar%
set myVar=000000%myvar%
echo 2: %myVar%
set myVar=%myVar:~-6%
echo 3: %myVar%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Stephan's method works. It can also be done using PowerShell formatting.

SET "N=123.4"
FOR /F %%n IN ('powershell -NoLogo -NoProfile -Command "([double]%N%).ToString('000000')"') DO (SET "NZ=%%n")
ECHO %NZ%
lit
  • 14,456
  • 10
  • 65
  • 119