Ratio calculation in progress

dibinc

New Member
Hi All,

Can somebody help me to calculate ratio in progress 4GL? for example I have a total count of 15 and used count is 9 and count may vary depending on other cases.
ratio from direct calculation is 9:15, but this need to be modify it to smaller level such as 3:5. Is there any direct calculation to achieve this instead of checking every digit on both side, appreciate for any help.
 
Sorry Tamhas I will try to explain below.

I have some scenario to calculate the two numbers ratio, like mentioned above 9 and 15 are numbers.
So on a high level the ratio is 9:15, I need to simplify it to smaller level that is 9/3 : 15/3 = 3:5. so the problem I am facing is the numbers may vary depending on some other cases like numbers may be multiple of 2s or 3s or 5s etc...
so my question is for calculating ratios what is the best method we can apply here.
 
You need to search for reducing a fraction.

The following code adapted from some random JavaScript code found on the web (gcd = greatest common divisor):

Code:
function gcd integer ( int, int ) forward.

function reduceFraction returns logical (
    x as int,
    y as int
):

    def var d as int no-undo.

    assign 
        d = gcd( x, y )

        x = x / d
        y = y / d
        .

    message substitute( 'x = &1, y = &2', x, y ).
end function.

function gcd returns integer (
  a as int,
  b as int
):

    if b = 0 then
        return a.

    return gcd( b, a modulo b ).

end function.

reduceFraction( 16, 10 ).
reduceFraction( 9, 15 ).

Watch it run at ProgressAblDojo
 
Thanks Stefan for your quick and good solution, I tested few scenarios and it seems working as expected.
Once again thanks for your time and help.
 
Back
Top