# Accurate Math in Batch

June 11, 2009 at 09:55:36
Specs: Windows XP, 2.8 ghz / 2 gig
 I'm trying to convert file sizes from bytes into the largest unit with a whole number. So 1025 bytes i'd like to convert to kilobytes and 2197152 would be converted to megabytes.I was well on my way with an if greater than 1023 divide by 1024, and then repeat that until it's less than 1024. But I found out set /a rounds to whole numbers.Is there a simple way to get around this or a cmd line program to do accurate math? Now that I say that I just learned how to use arguments with a VB program. I guess I could code up a simple VB program to do the math and return a value and unit. Is there a way to return a value back to the batch script or would i just have to use a temp file?Any other ideas are greatly appreciated.

See More: Accurate Math in Batch

#1
June 11, 2009 at 11:15:23
 Well I got my VB program to convert any numeric input into b, kb, mb, or gb. But I can't get the batch file to load the result. I have the VB program make a txt file with the result.```login to ftp and create list~ %5 is the file size in bytes if exist list~ ( echo %date% %time% >> "upload list.txt" echo. >> "upload list.txt" for /f "usebackq tokens=*" %%a in (list~) do ( call :write %%a ) echo. >> "upload list.txt" ) del list~ :write if not [%5] == [] ( start /w convert.exe %5 set /p size=> "upload list.txt" set size= ) goto :eof ```I ran the following chunk by itself in another batch script and it echoed the result. So for some reason when it's with the rest of the script it doesn't load the file. start /w convert.exe %5 set /p size=

Report •

#2
June 11, 2009 at 14:35:34
 I have a huge batch file that can do that, I've posted it here rather that scrolling the post by ~500 lines. The binary components aren't finished yet but do work for unsigned numbers.The numbers returned will be to ten decimal places, if needed, for division and decimal numbers can be input for operation. I'm sure you can figure out how to do your rounding based on the decimal.The posting on pastebin will last for one month, please don't edit it unless your fixing something.

Report •

#3
June 11, 2009 at 17:54:08
 if you have Python on Windows```import sys bytes = sys.argv[1] bytes = float(bytes) if bytes >= 1099511627776: terabytes = bytes / 1099511627776 size = '%.10fT' % terabytes elif bytes >= 1073741824: gigabytes = bytes / 1073741824 size = '%.10fG' % gigabytes elif bytes >= 1048576: megabytes = bytes / 1048576 size = '%.10fM' % megabytes elif bytes >= 1024: kilobytes = bytes / 1024 size = '%.10fK' % kilobytes else: size = '%.10fb' % bytes print size ```output```C:\test>python test.py 1024 1.0000000000K C:\test>python test.py 102444324242 95.4087118078G ```here's an executableusage:c:\> convert.exe 123456788or in batchfor /F %%a in ('convert.exe 12323131') do (echo %%a)

Report •

Related Solutions