Solved how to devision and rounding in strings?

November 4, 2016 at 06:02:18
Specs: Windows 64, Core i7 2GHz/16GB
Hi all,

I'm working with a tool which is exactly like windows cmd. my input text file is :

age=min;age_1D_param_min_64;meas_time =

My batch file reads the lines from this input and splits the lines by “ ; ” then executes a function with the token and age file from the second field and after all parses the output for the third line. now I have problem with one string in out.txt which is

dActual_age =31.9999

in the following you see my current batch file

setlocal EnableDelayedExpansion

for /f "tokens=1,2,3* delims=;" %%a in (input.txt) do (
REM create token file
echo.%%a>current.tok

sinoparam -p D:\product\%%b 0x0100001F > out.txt

for /f %%y in ('findstr /C:"%%c" out.txt ^| sed "s/.*%%c .............. ) do SET RESULT=%%y

echo.%%a;%%b;%%c;!RESULT!>>finaloutput.csv
)
GOTO :EOF

in my batch file I want to do the following:
-find this string by findstr/C
-if the value is lower than 1000 round it and show the result
-if it’s greater than 1000 first divide it by 32 and then round the result
Does anyone know how I can do this ?
thanks for any help!
Ela

message edited by elaheh


See More: how to devision and rounding in strings?

Reply ↓  Report •


#1
November 8, 2016 at 05:23:03
SInce batch doesn't do floating-point values, division (and math involving decimals) isn't really a great idea, but since you want the values rounded I suppose it's not a big issue.

When dividing with batch, the returned value will always be rounded downwards, so if it would return the float "29.998", then batch would round it down to 29.

@Echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=." %%A in ("%value%") do (
	if %%A LSS 1000 (
		set int=%%A
		if not "%%B"=="" (
			set decimal=%%B
			set decimal=!decimal:~0,1!
			if !decimal! GEQ 5 (
				set /a int+=1
			)
		)
	) else (
		if %%A GTR 1000 (
			set /a int=%%A/32
		)
	)
	echo !int!
)
pause

This code rounds the value upwards if the decimal is higher than or equal to .5
If you want it to round downwards instead (or not round at all, just display the integer which would be the same thing), just use this:
@Echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=." %%A in ("%value%") do (
	if %%A LSS 1000 (
		set int=%%A
	) else (
		if %%A GTR 1000 (
			set /a int=%%A/32
		)
	)
	echo !int!
)
pause

As for finding the value, I suggest this:

for /f "tokens=*" %%A in ('findstr /C:"dActual_age" inputfile.txt') do (
	for /f "tokens=2 delims==" %%B in ("%%A") do set value=%%B
)

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •

#2
November 9, 2016 at 11:15:53
@RainBawZ thanks for your answer!it works correct and exactly what I want for dealing with floats. now I cannot get my outputs echo.%%a;%%b;%%c;!int!
my current batch using the second code you sent:

setlocal EnableDelayedExpansion
for /f "tokens=1,2,3* delims=;" %%a in (input.txt) do (
REM create token file
echo.%%a>current.tok
sinoparam -p D:\product\%%b 0x0100001F > out.txt
for /f %%y in ('findstr /C:"%%c" out.txt ^| sed "s/.*%%c \([^>]*\).*/\1/i"') do SET RESULT=%%y
for /f "tokens=1,2 delims=." %%A in ("%y%") do (
if %%A LSS 1000 (
set int=%%A
) else (
if %%A GTR 1000 (
set /a int=%%A/32
)
)
echo !int!
)
echo.%%a;%%b;%%c;!int!>>finaloutput.csv
)
GOTO :EOF

Do you know what is the problem that I cannot get echo.%%a;%%b;%%c;!int!>>finaloutput.csv?


Reply ↓  Report •

#3
November 9, 2016 at 14:09:43
✔ Best Answer
for /f %%y in ('findstr /C:"%%c" out.txt ^| sed "s/.*%%c \([^>]*\).*/\1/i"') do SET RESULT=%%y

Right here you're setting RESULT to be the value of %%y, and then you close the loop, meaning %%y cannot be used further on.

for /f "tokens=1,2 delims=." %%A in ("%y%") do (

here you tell it to parse a variable called %y%, yet I don't see any variable with that name. If you mean %%y, you can't use that since you closed the loop that created it, however since you set RESULT to be the value of %%y, you can use !RESULT! instead of %y%.

Hopefully this resolves your issue.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •
Related Solutions


Ask Question