Hi Paul. I have tested your equation and mine in a double for loop going from a questionCount of 10 to 30 in the outer loop and a correctCount of 1 to the current questionCount

in the inner loop. This let me test all the permutations.

Your code worked well in almost all loop iterations. the only discrepancy was with the following values:

1) correctCount 2 and questionCount 16 gave a fraction 2/16 = 0.125 - my equation gave this as 13% whereas your equation gave it as 12%

2) correctCount 10 and QuestionCount 16 gave a fraction 10/16 = 0.625 - my equation gave this as 63% whereas your equation gave it 62%

3) correctCount 3 and questionCount 24 gave a fraction 3/24 = 0.125 - my equation gave this as 13% whereas your equation gave it 12%

4) correctCount 15 and questionCount 24 gave a fraction 15/24 = 0.625 - my equation gave this as 63% whereas your equation gave 62%

You can see two of the problem values are 0.125 and 0.625.

Anything that can be done to correct this rounding error?

None

0 Points

13 Posts

## Converting a Delphi 6 Function to c#

May 17, 2017 01:45 PM|HamtekBusinessSoftware|LINK

Hi guys. I have this simple function in Delphi

function Round45 (Value : Real; RPower : Integer) : Real;

begin

Result := Trunc (Value * Power (10, RPower) + 0.5)/Power (10, RPower);

end;

It's purpose was to provide a result in the range from 0 to 100.

I am attempting to convert that function to c#

I have tried using Math.Round but that produces a double - I require an integer from 0 to 100.

Any help would be appreciated.

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 17, 2017 04:09 PM|HamtekBusinessSoftware|LINK

Oh, it seems I can just use

So, for example, if I have 20 questions taken and the user obtains 9 correct answers, the code produces 45 which is just what I required.

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 18, 2017 09:38 AM|HamtekBusinessSoftware|LINK

My previous works well for 2 and 20 questions, but fails with say 6 questions correct out of 13 questions.

This new code corrects that error

Note the 100f which was all that was missing form my first attempt and removes the need for a double cast.

Hope this info helps someone.

Participant

820 Points

427 Posts

## Re: Converting a Delphi 6 Function to c#

May 22, 2017 04:46 AM|PaulTheSmith|LINK

Have you worked through, on paper, what your code is doing?

If correctCount = 6 and questionCount = 13 then this is what happens (what is the type of these variables???)

First multiply correctCount by 100 to get

600

Now divide by 13 to get, approximately

46.15385

Now you multiply by Mth.Pow(10,10) which is a

hugenumber to get461,538,461,538.462

Now you add 0.5 to get

461,538,461,538.962

now divide by the same huge number to get back to the number you started with (except for an amount less than the width of an electron)

46.1538461538962

and then you round it to get

46

and then you convert to Int32 to get

46

Wow! That was a long journey.

If the input variables are integers then

int result = correctCount*100/questionCount

will give the same result.

If they are not integers then what are they?

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 22, 2017 09:28 AM|HamtekBusinessSoftware|LINK

Hi Paul.

The inputs are integers, so where as:

using correctCount = 6 and questionCount = 13

int result = (6 * 100) /13 gives a result of 46 which is correct when rounding 46.15

but, if you use correctCount = 7 and questionCount = 13

int rrsult = (7 * 100)/13 gives a result of 53 however rounding 53.84 shows the result should be 54.

My code gives the correct answer of 54.

So I don't think I can use simple integer maths.

If you have a simple formula the gives 5/4 rounding of integers, please let me know.

Participant

820 Points

427 Posts

## Re: Converting a Delphi 6 Function to c#

May 24, 2017 10:32 PM|PaulTheSmith|LINK

var result = Math.Round(correctCount*100f/questionCount);

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 25, 2017 06:39 AM|HamtekBusinessSoftware|LINK

Thanks Paul. That seems to work. I shall have to fully test it.

Thanks for the help and your time.

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 25, 2017 11:04 AM|HamtekBusinessSoftware|LINK

Hi Paul. I have tested your equation and mine in a double for loop going from a questionCount of 10 to 30 in the outer loop and a correctCount of 1 to the current questionCount

in the inner loop. This let me test all the permutations.

Your code worked well in almost all loop iterations. the only discrepancy was with the following values:

1) correctCount 2 and questionCount 16 gave a fraction 2/16 = 0.125 - my equation gave this as 13% whereas your equation gave it as 12%

2) correctCount 10 and QuestionCount 16 gave a fraction 10/16 = 0.625 - my equation gave this as 63% whereas your equation gave it 62%

3) correctCount 3 and questionCount 24 gave a fraction 3/24 = 0.125 - my equation gave this as 13% whereas your equation gave it 12%

4) correctCount 15 and questionCount 24 gave a fraction 15/24 = 0.625 - my equation gave this as 63% whereas your equation gave 62%

You can see two of the problem values are 0.125 and 0.625.

Anything that can be done to correct this rounding error?

None

0 Points

13 Posts

## Re: Converting a Delphi 6 Function to c#

May 25, 2017 11:21 AM|HamtekBusinessSoftware|LINK

Ahh, all it needed was the MidpointRounding parameter