Output numeric values with 2 digits after fraction?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Output numeric values with 2 digits after fraction?

#1 Post by pstein » 20 Jan 2015 07:08

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output numeric values with 2 digits after fraction?

#2 Post by Squashman » 20 Jan 2015 07:28

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.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Output numeric values with 2 digits after fraction?

#3 Post by pstein » 20 Jan 2015 07:41

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?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output numeric values with 2 digits after fraction?

#4 Post by Squashman » 20 Jan 2015 08:10

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.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Output numeric values with 2 digits after fraction?

#5 Post by ShadowThief » 20 Jan 2015 08:25

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

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Output numeric values with 2 digits after fraction?

#6 Post by pstein » 20 Jan 2015 09:24

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?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Output numeric values with 2 digits after fraction?

#7 Post by ShadowThief » 20 Jan 2015 10:03

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%

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Output numeric values with 2 digits after fraction?

#8 Post by ShadowThief » 20 Jan 2015 10:05

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.

Aacini
Expert
Posts: 1927
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Output numeric values with 2 digits after fraction?

#9 Post by Aacini » 20 Jan 2015 10:46

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
Last edited by Aacini on 20 Jan 2015 10:57, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output numeric values with 2 digits after fraction?

#10 Post by Squashman » 20 Jan 2015 10:56

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Output numeric values with 2 digits after fraction?

#11 Post by dbenham » 20 Jan 2015 12:15

PowerShell 1 liner:

Code: Select all

for /f %%A in ('powershell -command "& {[math]::round(7/2,2)}"') do set "bbb=%%A"


Dave Benham

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Output numeric values with 2 digits after fraction?

#12 Post by ShadowThief » 20 Jan 2015 12:16

You can do that?!

Man, I really need to learn Powershell...

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Output numeric values with 2 digits after fraction?

#13 Post by Compo » 20 Jan 2015 12:30

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output numeric values with 2 digits after fraction?

#14 Post by Squashman » 20 Jan 2015 12:48

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Output numeric values with 2 digits after fraction?

#15 Post by dbenham » 20 Jan 2015 14:17

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

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

Post Reply