Output numeric values with 2 digits after fraction?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Output numeric values with 2 digits after fraction?

#16 Post by Squashman » 20 Jan 2015 14:41

dbenham wrote:jEval.bat

How many more of these Hybrid Scripts do you got up your sleeve?

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: Output numeric values with 2 digits after fraction?

#17 Post by catalinnc » 22 Jan 2015 17:58

simple and fast stuff limited to range of -/+ 214748364

Code: Select all

@echo off & cls

setlocal disabledelayedexpansion


rem setting part
rem use only decimal numbers
rem in range of -/+ 214748364
rem you can change decimal sign to ,


set "_1st_number=-21474836"
set "_2nd_number=330"


set "_decimal_sign=,"


rem testing part

set /a "_test=%_1st_number%"
if %errorlevel% neq 0 (echo( & echo(first number is out of range & echo( & pause & goto :eof)
if [%_1st_number%] neq [%_test%] (echo(first number is out of range & echo( & pause & goto :eof)
if %_test% gtr 214748364 (echo(first number is out of range & echo( & pause & goto :eof)
if %_test% lss -214748364 (echo(first number is out of range & echo( & pause & goto :eof)

set /a "_test=%_2nd_number%"
if %errorlevel% neq 0 (echo( & echo(second number is out of range & echo( & pause & goto :eof)
if [%_2nd_number%] neq [%_test%] (echo(second number is out of range & echo( & pause & goto :eof)
if %_test% gtr 214748364 (echo(second number is out of range & echo( & pause & goto :eof)
if %_test% lss -214748364 (echo(second number is out of range & echo( & pause & goto :eof)

set "_test="


rem math part

if %_1st_number% lss 0 (if %_2nd_number% gtr 0 (set "_result_sign=-"))
if %_2nd_number% lss 0 (if %_1st_number% gtr 0 (set "_result_sign=-"))
if %_1st_number% lss 0 (set "_1st_number=%_1st_number:~1%" & set "_1st_number_sign=-")
if %_2nd_number% lss 0 (set "_2nd_number=%_2nd_number:~1%" & set "_2nd_number_sign=-")

set /a "_value1=(%_1st_number%)/(%_2nd_number%)", "_modulus1x10=((%_1st_number%)%%(%_2nd_number%))*(10)"
if %errorlevel% neq 0 (echo( & pause & goto :eof)
set /a "_value2=(%_modulus1x10%)/(%_2nd_number%)", "_modulus2x10=((%_modulus1x10%)%%(%_2nd_number%))*(10)"
set /a "_value3=(%_modulus2x10%)/(%_2nd_number%)", "_modulus3x10=((%_modulus2x10%)%%(%_2nd_number%))*(10)"
set /a "_value4=(%_modulus3x10%)/(%_2nd_number%)"

set "_decimals=%_value2%%_value3%"
if %_value4% gtr 5 (set /a "_decimals+=1")
if %_decimals% gtr 99 (set /a "_value1+=1" & set "_decimals=00")

set "_result=%_result_sign%%_value1%%_decimal_sign%%_decimals%"

echo(%_1st_number_sign%%_1st_number%/%_2nd_number_sign%%_2nd_number%=%_result%
echo(

endlocal

pause

exit /b

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

Re: Output numeric values with 2 digits after fraction?

#18 Post by pstein » 24 Jan 2015 01:49

Compo wrote: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.


Hmm, this does not seem to work:

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

outputs 1,12 (=wrong) instead of 1,13 (=correct)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Output numeric values with 2 digits after fraction?

#19 Post by foxidrive » 24 Jan 2015 05:32

pstein wrote:
Compo wrote: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.


Hmm, this does not seem to work:

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

outputs 1,12 (=wrong) instead of 1,13 (=correct)


If you look closely it is not the same code as you quoted from Compo.

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

Re: Output numeric values with 2 digits after fraction?

#20 Post by Compo » 24 Jan 2015 07:49

pstein wrote:
Compo wrote: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.


Hmm, this does not seem to work:

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

outputs 1,12 (=wrong) instead of 1,13 (=correct)


See Here

Code: Select all

C:\Users\Compo>For /f %A In ('PowerShell -c "[math]::round(9/8,2)"') Do @Echo(%A
1.12

C:\Users\Compo>For /f %A In ('PowerShell -c "[math]::round(9/8,2,1)"') Do @Echo(%A
1.13

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

Re: Output numeric values with 2 digits after fraction?

#21 Post by pstein » 24 Jan 2015 08:01

Ok, little mistake from me :-)

Thank you

Post Reply