Page 1 of 2
Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 07:08
by pstein
Assume I have a calculation like
set /a aaa=7 / 3
echo aaa=%aaa%
This gives:
aaa=2
How can I show two digits after the fractio separator as well? For the example below how to show
aaa=2,33
?
The original value should be rounded. So
7,3549999 should be displayed as 7,35
7,3551234 should be displayed as 7,36
Peter
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 07:28
by Squashman
Batch can natively only handle integers for math. It cannot output the decimals. If you need decimals do Google search for Judago or code this in a different language.
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 07:41
by pstein
Squashman wrote:Batch can natively only handle integers for math. It cannot output the decimals. If you need decimals do Google search for Judago or code this in a different language.
Hmm, this is a bad surprise.
Does this apply only to the output or to intermediate in-memory values as well?
Is there a workaround?
E.g. Add a "100 * ......." to the formula, then convert result to string and insert a comma at the third last position?
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 08:10
by Squashman
Well this is a bummer. Judago doesn't have his site up anymore. He had some great batch files for doing decimal precision math and for doing math with numbers larger than 32 bit. He has occassionally popped his head in here at DosTips. Not sure what he is doing these days.
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 08:25
by ShadowThief
You can throw the needed math into a powershell command wrapped in a for loop, if you have PowerShell installed (you probably do).
Code: Select all
for /F %%A in ('powershell 7/3') do set aaa=%%A
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 09:24
by pstein
ShadowThief wrote:You can throw the needed math into a powershell command wrapped in a for loop, if you have PowerShell installed (you probably do).
Code: Select all
for /F %%A in ('powershell 7/3') do set aaa=%%A
Hey, looks better but....
1.) how to restrict it to two digits after comma?
Is there a powershell option which must be set?
Otherwise: How to I cut the string in %aaa% to two digits after comma?
2.) Your solution does not work with variables: "," separates in european style whole numbers from fractioned part
set bbb=3,23
set ccc=5,223
for /F %%A in ('powershell %bbb%/%ccc%') do set aaa=%%A
Or did I miss something?
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 10:03
by ShadowThief
In regular powershell, you'd be able to use "{0:N2}" -f $(7/3) and get 2.33
Unfortunately, I can't seem to get it to work in a command prompt one-liner. That said, you can take a second for loop, split it at the decimal, crop the back half to two decimal points, and recombine the string.
Code: Select all
@echo off
set bbb=7
set ccc=3
for /F "tokens=1,2 delims=." %%A in ('powershell %bbb%/%ccc%') do (
set ipart=%%A
set fpart=%%B
set aaa=%ipart%.%fpart:~0,2%
)
echo %aaa%
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 10:05
by ShadowThief
Oh, european decimals. I don't have a way of testing that, but it should be similar. You may have to convert the commas to periods first.
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 10:46
by Aacini
I suggest you to use
JScript instead of PowerShell. I think it is simpler, but certainly it is faster!
Code: Select all
C:\> type test.bat
@if (@CodeSection == @Batch) @then
@echo off
set bbb=3,23
set ccc=5,223
for /F %%A in ('Cscript //nologo //E:JScript "%~F0" "%bbb:,=.% / %ccc:,=.%"') do set aaa=%%A
echo aaa = %aaa:.=,%
for /F %%A in ('Cscript //nologo //E:JScript "%~F0" "(%bbb:,=.% / %ccc:,=.%).toFixed(2)"') do set aaa=%%A
echo aaa = %aaa:.=,%
goto :EOF
@end
WScript.Echo(eval(WScript.Arguments.Item(0)));
C:\> test
aaa = 0,618418533409918
aaa = 0,62
Antonio
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 10:56
by Squashman
Painfully slow but seems to work.
Code: Select all
for /F %%A in ('powershell 7/3') do set aaa=%%A
for /F %%A in ('powershell "[math]::round(%aaa%,2)"') do set bbb=%%A
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 12:15
by dbenham
PowerShell 1 liner:
Code: Select all
for /f %%A in ('powershell -command "& {[math]::round(7/2,2)}"') do set "bbb=%%A"
Dave Benham
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 12:16
by ShadowThief
You can do that?!
Man, I really need to learn Powershell...
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 12:30
by Compo
Similar to Dave's
Code: Select all
@For /F %%A In ('PowerShell -C "[math]::round(7/3,2,1)"') Do @Echo(%%A&Timeout 5
The 1 after the comma I use to make sure that the result is 'away from zero' midpoint rounded.
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 12:48
by Squashman
ShadowThief wrote:You can do that?!
Man, I really need to learn Powershell...
I got into it heavily back in 2008. I was starting to rewrite all my batch files into Powershell. Then I got disenchanted with the people I worked with and stopped development on all of it. And we basically never moved to Windows 7 until about 2011 or 2012 and I wasn't going to ask corporate I.T. to push down the Powershell install to every computer with XP. I was so happy with what I could do with it, but now I have pretty much forgotten everything.
I was even coding all of them for drag and drop input. I have no idea what I did with all my scripts. Lost forever I suppose. The guy I collaborated with online to create a lot of them dropped off the face of the earth. We were the main batch and powershell contributors on TechGuy.org back then. One day he was gone. I sure miss him.
Re: Output numeric values with 2 digits after fraction?
Posted: 20 Jan 2015 14:17
by dbenham
I agree with Aacini that JScript is a better (faster) option than PowerShell.
I like to use a simple hybrid JScript/batch utility called jEval.bat to evaluate simple JScript expressions. I put jEval.bat somewhere within my PATH.
jEval.batCode: Select all
@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment
@goto :batch
::************ Documentation ***********
:::
:::jEval JScriptExpression [/N]
:::jEval /?
:::
::: Evaluates a JScript expression and writes the result to stdout.
:::
::: A newline (CR/LF) is not appended to the result unless the /N
::: option is used.
:::
::: The JScript expression should be enclosed in double quotes.
:::
::: JScript string literals within the expression should be enclosed
::: in single quotes.
:::
::: Example:
:::
::: call jEval "'5/4 = ' + 5/4"
:::
::: Output:
:::
::: 5/4 = 1.25
:::
============ :Batch portion ================
@echo off
if "%~1" equ "" (
call :err "Insufficient arguments"
exit /b
)
if "%~2" neq "" if /i "%~2" neq "/N" (
call :err "Invalid option"
exit /b
)
if "%~1" equ "/?" (
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do echo(%%A
exit /b
)
cscript //E:JScript //nologo "%~f0" %*
exit /b
:err
>&2 echo ERROR: %~1. Use jeval /? to get help.
exit /b 1
************ JScript portion ***********/
if (WScript.Arguments.Named.Exists("n")) {
WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}
This problem can then be solved using:
Code: Select all
for /f %%A in ('jeval "(7/3).toFixed(2)"') do set "bbb=%%A"
Dave Benham