Page 1 of 1

help cbm calculator

Posted: 25 Jun 2015 06:39
by xhai
hi guys, I have a little problem. how do I create a simple script like a calculator. The formula was L x W x H in cm / 1000000 = value.cbm

to do
-two decimal place
- ability to add 2 or more entries and add the total cbm.
-convert meter to centimeter to compute the cbm

Re: help cbm calculator

Posted: 25 Jun 2015 13:02
by aGerman
a simple script

There is no simple way. The command processor can only recognize integral numbers from -2147483648 to 2147483647. Also the result of any calculation will be an integer. All decimals are automatically eliminated. There are several work-arounds using string manipulations that are all more or less complicated. A hybrid script could be a little easier:
.bat

Code: Select all

@if (@a)==(@b) @end /*

@echo off &setlocal
set "formula=2/3"
for /f %%i in ('cscript //nologo //e:jscript "%~fs0" "%formula%"') do set "result=%%i"
echo %result%
pause

exit /b &rem */ WScript.Echo(parseFloat(eval(WScript.Arguments.Item(0))).toFixed(2));

That code uses JScript. As you can see the expression in variable formula will be evaluated, rounded and assigned to variable result.

Regards
aGerman

Re: help cbm calculator

Posted: 25 Jun 2015 15:48
by Aacini
Although the arithmetic operations of SET /A command can only manage integers, it is relatively simple to achieve operations with a *fixed number* of decimals using the method described at this post. However, you must check that the range of values allowed by the 32-bits integers, less the part used for the decimals, is enough for your needs. Please, post a small table as example of input and output values and indicate which ones would be the maximum possible input values.

Also, it is important that you define if the L, W and H input values have also 2 decimals or just the calculated result.

Antonio

Re: help cbm calculator

Posted: 27 Jun 2015 09:44
by xhai
here's some sample input and output

ex. H =1280 W =223 L =134
l x w x h / 1000000 = 38.24 cbm

ex l = 90 x w = 110 h = 36
l x w x h / 1000000 = 0.85
H =1280 W =223 L =134
l x w x h / 1000000 = 38.24 cbm

0.85 + 38.24 = 39.09

Re: help cbm calculator

Posted: 27 Jun 2015 13:36
by Aacini

Code: Select all

@echo off
setlocal

set /A i=0, total=0
echo Enter L, W and H values separated by spaces (nothing to end):
:nextEntry
   set /A i+=1
   set "entry="
   set /P "entry=%i%- "
   if not defined entry goto endData
   for /F "tokens=1-3" %%a in ("%entry%") do set /A cbm=%%a*%%b*%%c/10000, total+=cbm
   echo    = %cbm:~0,-2%.%cbm:~-2% cbm
goto nextEntry
:endData
if %i% gtr 2 echo Total = %total:~0,-2%.%total:~-2%

Output example:

Code: Select all

C:\> test.bat
Enter L, W and H values separated by spaces (nothing to end):
1- 134 223 1280
   = 38.24 cbm
2-

C:\> test.bat
Enter L, W and H values separated by spaces (nothing to end):
1- 90 110 36
   = .35 cbm
2- 134 223 1280
   = 38.24 cbm
3-
Total = 38.59

You have an error in your example data; the cbm of 90, 110, 36 is 0.35, not 0.85.

Antonio

Re: help cbm calculator

Posted: 27 Jun 2015 22:26
by xhai
thank you very much Antonio ,
it's a very helpful tool in my work

Re: help cbm calculator

Posted: 28 Jun 2015 06:39
by xhai
small problem.. it accept strings how to i change it so it can only accept integers only..

Re: help cbm calculator

Posted: 28 Jun 2015 07:49
by foxidrive
You use a for /f loop to exclude numbers and then branch to your error routine, on the result.

I believe it was aGerman that posted the routine once more in the last week.

Re: help cbm calculator

Posted: 28 Jun 2015 12:19
by Aacini
xhai wrote:small problem.. it accept strings how to i change it so it can only accept integers only..


This is funny: your new "small problem" is much more complex than the 2 decimals original one! If you want that the "entry" variable contain just numbers, so the arithmetic operation don't mark any errors, then there are two possible ways to achieve that.

The first one is to read the entry with SET /P and then review the entry, mark an error if is wrong and repeat the process until the entry is correct. You may read possible methods to check if the entry contain just digits at Check variable is number post. However, in this case your requirement is more complex, because the program needs three numbers separated by spaces! Also, you must specify other points; for example, if the entry contains more than three numbers, do you want to ignore the rest or mark an error? Good luck with that... Of course, you could read just one value per line and check it, so you need to define a subroutine to check the number and call it three times.

Another possible solution is to restrict the entry so it can only accept numbers; more specifically, to just accept precisely three integers of a maximum of 4 digits each separated by a space. This method is more pleasant for the user and provide a correct entry always. You may use my ReadFormattedLine subroutine to achieve this in a very simple way; just change this line:

Code: Select all

   set /P "entry=%i%- "

... by this one:

Code: Select all

   call :ReadFormattedLine entry="##### ##### #####" /M "%i%- " /F

Of course, you must also include the code of the subroutine in your Batch file, that is "just" 145 lines long! :mrgreen:

Finally, I state the solution that, in my opinion, is the best suited for your needs. After this line:

Code: Select all

echo Enter L, W and H values separated by spaces (nothing to end):

... insert this one:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio

Re: help cbm calculator

Posted: 28 Jun 2015 22:28
by foxidrive
That is how to solve errors in batch scripts! :D

Aacini wrote:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio

Re: help cbm calculator

Posted: 28 Jun 2015 22:29
by foxidrive
That is how to solve errors in batch scripts! :D

Aacini wrote:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio



There are so many things that can break a batch file, if the input data is not as it is expected to be.