Comment detail

与えられた数字のケタ数 (Nested Flatten)
「文字列の反転」でバッチによる文字列長の算出方法を書いたので、それを応用しました。

念のため、引数が数値かどうかチェックしています。加えて、負の整数にも対応しました。

  e.g.
    C:\>figure 1000
    数字の桁数 : 4
    最大桁の位 : 1000

    C:\>figure 100
    数字の桁数 : 3
    最大桁の位 : 100

    C:\>figure 10
    数字の桁数 : 2
    最大桁の位 : 10

    C:\>figure 1
    数字の桁数 : 1
    最大桁の位 : 1

    C:\>figure -1
    数字の桁数 : 1
    最大桁の位 : 1

    C:\>figure -10
    数字の桁数 : 2
    最大桁の位 : 10

    C:\>figure -100
    数字の桁数 : 3
    最大桁の位 : 100

    C:\>figure -1000
    数字の桁数 : 4
    最大桁の位 : 1000

遅延環境変数展開を利用しているので、Windows NTでは動作しません。Windows XPで動作
を確認。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
:: figure.bat
@echo off
  setlocal enabledelayedexpansion
    set l=0
    set m=1
    set n=%1

    echo %n%|findstr /r "[^0-9\-]" >NUL 2>&1
    if %ERRORLEVEL% equ 0 goto :EOF

    if %n% lss 0 set /a n*=-1

    call :length %n% l
    echo 数字の桁数 : %l%

    set /a l-=1
    for /l %%i in (1,1,%l%) do set /a m=!m!*10
    echo 最大桁の位 : %m%
  endlocal
goto :EOF

:length
  setlocal
    set i=0
    set t=%1
    set t=%t:"=%

    :loop
      set t=%t:~1%
      set /a i+=1
    if not "%t%" == "" goto loop
  endlocal & set %2=%i%
goto :EOF

Index

Feed

Other

Link

Pathtraq

loading...