Output numeric values with 2 digits after fraction?
Moderator: DosItHelp
Output numeric values with 2 digits after fraction?
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
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?
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?
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?
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.
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Output numeric values with 2 digits after fraction?
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?
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?
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Output numeric values with 2 digits after fraction?
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.
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%
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Output numeric values with 2 digits after fraction?
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?
I suggest you to use JScript instead of PowerShell. I think it is simpler, but certainly it is faster!
Antonio
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
Last edited by Aacini on 20 Jan 2015 10:57, edited 1 time in total.
Re: Output numeric values with 2 digits after fraction?
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?
PowerShell 1 liner:
Dave Benham
Code: Select all
for /f %%A in ('powershell -command "& {[math]::round(7/2,2)}"') do set "bbb=%%A"
Dave Benham
-
- Expert
- Posts: 1167
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Output numeric values with 2 digits after fraction?
You can do that?!
Man, I really need to learn Powershell...
Man, I really need to learn Powershell...
Re: Output numeric values with 2 digits after fraction?
Similar to Dave'sThe 1 after the comma I use to make sure that the result is 'away from zero' midpoint rounded.
Code: Select all
@For /F %%A In ('PowerShell -C "[math]::round(7/3,2,1)"') Do @Echo(%%A&Timeout 5
Re: Output numeric values with 2 digits after fraction?
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?
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.bat
This problem can then be solved using:
Dave Benham
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.bat
Code: 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