user7127267
user7127267

Reputation:

How to create a number with N digits in Batch Script Windows?

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.

Upvotes: 1

Views: 4039

Answers (2)

lit
lit

Reputation: 16256

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%

Upvotes: 0

Stephan
Stephan

Reputation: 56188

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%

Upvotes: 2

Related Questions