Page 1 of 1
Sum of two decimal values
Posted: 05 Oct 2021 09:26
by sark
The below set variable outputs the sum of 6 and 2. This works fine, but only works with whole numbers. How, if possible, could I output values with decimal points. ie: 6.2+2.4 outputting 8.6.
Set /a S=6×2
%S%
Thanks
Mark
Re: Sum of two decimal values
Posted: 05 Oct 2021 09:54
by Squashman
for /F "delims=" %%G IN ('powershell -command "2.4 + 6.2"') do set "result=%%G"
Re: Sum of two decimal values
Posted: 05 Oct 2021 09:56
by atfon
The command processor has a limitation when it comes to floating point arithmetic. Have a look at this workaround:
https://www.dostips.com/DtTipsArithmeti ... atingPoint
Note: You can get your decimal back with string manipulation on the result.
Re: Sum of two decimal values
Posted: 05 Oct 2021 10:41
by sark
Thanks for the quick responses.
@atfon. l was reading the workaround you linked whilst you were replying. What exactly do you mean by string manipulation to recover the decimal point?
Thanks
Mark
Re: Sum of two decimal values
Posted: 05 Oct 2021 11:28
by atfon
sark wrote: ↑05 Oct 2021 10:41
What exactly do you mean by string manipulation to recover the decimal point?
Thanks
Mark
Hello Mark. You can extract the first digit and the last digit:
https://www.dostips.com/DtTipsStringMan ... LeftString
For the first digit, set "first=%str:~-1%" and for the last set "last=%str:~0,1%". Then you can set the entire term as set "whole=%first%.%last.%"
There are likely easier ways to do this that the Experts on this site could point out to you. For example, Squashman's powershell example or VBScript.
Re: Sum of two decimal values
Posted: 05 Oct 2021 15:37
by sark
Thanks for that.
I'm incorperating into ffmpeg scripts so will take a closer look at the best approach for my needs.
Mark