Suddenly saw a question about rounding in the post!
Thought to myself, CMD doesn't support rounding operations.
But then when I turned to look at other posts, I had an idea and came up with the following algorithm..
Now I'm writing my algorithm below. Hope to progress together with the experts.
Also hope experts can streamline their code.
——————————————————————————————————
Idea:
Take the remainder of division. If the remainder minus half of the dividend is greater than 0, it means this number is rounded up. Otherwise, it's rounded down.
Added a judgment on the remainder, because the remainder can only be 2n or 2N+1. If it's 2n+1, then dividing this number by 2 meets the conditions for rounding. If not, continue with the following judgment
——————————————————————————————————
@echo off & SetLocal EnableDelayedExpansion
set /a divisor=25
set /a dividend=10
set /a remainder=%divisor% %% %dividend%
set /a reference_value=%dividend%/2
set /a reference_value1=%dividend%-%reference_value%*2
set /a carry=%remainder%-(%reference_value%+%reference_value1%)
echo %reference_value%,%reference_value1%,%carry%
if %carry% LSS 0 (
echo The result of %divisor% ÷ %dividend% is rounding down!
set /a result=%divisor%/%dividend%
) else (
echo The result of %divisor% ÷ %dividend% is rounding up!
set /a result=%divisor%/%dividend%+1
)
echo The answer is:%result%
pause
--------------------------------------------------------------------------------------------
Here I just threw out a brick.. but attracted a piece of jade out..
Everyone take a look at bjsh's code, it's getting more and more perfect each time.
Now it can basically handle division operations of any number of digits and ensure precision.
Everyone give bjsh more points :)
[ Last edited by flyinspace on 2007-4-1 at 08:33 PM ]
Thought to myself, CMD doesn't support rounding operations.
But then when I turned to look at other posts, I had an idea and came up with the following algorithm..
Now I'm writing my algorithm below. Hope to progress together with the experts.
Also hope experts can streamline their code.
——————————————————————————————————
Idea:
Take the remainder of division. If the remainder minus half of the dividend is greater than 0, it means this number is rounded up. Otherwise, it's rounded down.
Added a judgment on the remainder, because the remainder can only be 2n or 2N+1. If it's 2n+1, then dividing this number by 2 meets the conditions for rounding. If not, continue with the following judgment
——————————————————————————————————
@echo off & SetLocal EnableDelayedExpansion
set /a divisor=25
set /a dividend=10
set /a remainder=%divisor% %% %dividend%
set /a reference_value=%dividend%/2
set /a reference_value1=%dividend%-%reference_value%*2
set /a carry=%remainder%-(%reference_value%+%reference_value1%)
echo %reference_value%,%reference_value1%,%carry%
if %carry% LSS 0 (
echo The result of %divisor% ÷ %dividend% is rounding down!
set /a result=%divisor%/%dividend%
) else (
echo The result of %divisor% ÷ %dividend% is rounding up!
set /a result=%divisor%/%dividend%+1
)
echo The answer is:%result%
pause
--------------------------------------------------------------------------------------------
Here I just threw out a brick.. but attracted a piece of jade out..
Everyone take a look at bjsh's code, it's getting more and more perfect each time.
Now it can basically handle division operations of any number of digits and ensure precision.
Everyone give bjsh more points :)
[ Last edited by flyinspace on 2007-4-1 at 08:33 PM ]
知,不觉多。不知,乃求知

DigestI