Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo off
- :# FormatNum function by T3RRY ; Created: 25th July 2021
- :# Purpose: Function to Format numbers up to 1485 digits long with coma's IE:
- :# 10 = 10 ; 100 = 100 ; 1000 = 1,000 ; 10000 = 10,000 ; 100000 = 100,000 ; 1000000 = 1,000,000
- :# -S and -0 switches accepted as third arg to prepend with whitespice or leading zero's respectively.
- Setlocal EnableExtensions
- %= Examples =%
- :# 0 prefixed
- Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[1]1 -0
- Call :FormatNum 4071 rv[1]2 -0
- Call :FormatNum 20 rv[1]3 -0
- Call :FormatNum 3 rv[1]4 -0
- Set rv[1]
- :# Whitespace prefixed
- Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[2]1 -S
- Call :FormatNum 4071 rv[2]2 -S
- Call :FormatNum 20 rv[2]3 -S
- Call :FormatNum 3 rv[3]4 -S
- Set rv[2]
- :# No Prefix
- Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[3]1
- Call :FormatNum 4071 rv[3]2
- Call :FormatNum 20 rv[3]3
- Call :FormatNum 3 rv[3]4
- Set rv[3]
- Endlocal
- Goto :Eof
- :FormatNum <Int> <ReturnVar> [-0|-S]
- If not defined FormatDelim Set "FormatDelim=," %= Allow default ',' delim to be overridden =%
- If /I "%~2"=="-0" Exit /b 3 %= Errorlevel 3 - arg2 incorrectly occuipied by arg3 =%
- If /I "%~2"=="-S" Exit /b 3
- If "%~2"=="" Exit /b 2 %= Errorlevel 2 - arg2 missing =%
- If "%~1"=="" Exit /b 1 %= Errorlevel 1 - all args missing =%
- Setlocal EnableDelayedExpansion
- set "tmp=.%~1" %= No Leading zeros =%
- If "%~3"=="-0" set "tmp=.00%~1" %= Leading zeros =%
- If /I "%~3"=="-S" set "tmp=. %~1" %= Leading whitespace =%
- %= Approximate Max string length as a multiple of 3 to reduce execution time =%
- For %%i in (1485 918 567 351 216 135 81 54 27 18 9)Do If "!tmp:~%%i,1!"=="" ( Set /A "Max=%%i" )
- For /l %%n in (!Max! -3 0)Do (
- For /f "Delims=" %%o in ('Set /A %%n+3')Do If not "%%n"=="0" (
- set "Size=!Size!%FormatDelim%!tmp:~-%%o,-%%n!"
- )Else set "Size=!Size!%FormatDelim%!tmp:~-3!"
- )
- %= iterations below are required to remove all paired comas. =%
- For /l %%i in (0 1 8)Do set "Size=!Size:%FormatDelim%%FormatDelim%=%FormatDelim%!"
- %= remove excess leading chars =%
- set "Size=!Size:~1!"
- set "Size=!Size:.00%FormatDelim%=!"
- set "Size=!Size:.0%FormatDelim%=!"
- set "Size=!Size:. %FormatDelim%=!"
- set "Size=!Size:. %FormatDelim%=!"
- set "Size=!Size:.%FormatDelim%=!"
- set "Size=!Size:.=!"
- Endlocal & Set "%~2=%Size%"
- Exit /B 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement