percentage

P

Peter

Hi

is there a "percentage type" in c#, or how do I specify that a
parameter to my method may only take integer values from 0 to 100?

For example:

public bool Reject(Guid guid, Percent percent)
{
// do stuff...
return true;
}


Would I need to create my own type for this; or just check an "int"
value supplied and reject it if it is outside my bounds?

Thanks,
Peter

--
 
P

Peter Duniho

[...]
Would I need to create my own type for this; or just check an "int"
value supplied and reject it if it is outside my bounds?

If you want a type that can never have a value other than 0 to 100,
yes...you'd need to create your own type.

However, I do think that simply range-checking the parameter is fine. You
can throw a new ArgumentException, or handle the error some other way if
you don't think that's appropriate.

Pete
 
K

Kevin Spencer

Percentage is a way of representing a ratio. A ratio represents the
relationship in quantity/size of 2 numbers, in which one number represents a
base value and the other represents a value that is relative to that value.
For example, 2/3 is a ratio, in which 3 represents the base value, and 2
represents the value that is relative to 3. However, in a ratio, the
relative value does not necessarily have to be smaller than the base value.
For example, 3/2 represents a ratio in which the relative value is greater
than the base value.

Percentage is a way of representing any ratio as having a base value of 100.
Because a ratio can be expressed as a fraction or rational number, and any
fraction can have the denominator changed to any value by multiplying the
numerator and denominator by the same number, any ratio can be expressed as
a percentage by multiplying the numerator and denominator by (100 /
denominator), since the value of (100 / denominator) multiplied by the
denominator equals 100. So, for example, in the case of 2/3, you divide 100
by 3, getting 33.3333 as a result, and multiply this times the numerator (2
* 33.3333 = 66.6666) and the denominator (3 * 33.3333 = 100), you get
66.6666%, or 66.6666/100.

However, a percentage can be greater than 100, as in the case of 3/2, which
would be 150%. This is also equivalent to 1.5, or simply "3 divided by 2".

So, a percentage is a rational number, which might be represented by an
integer, a float, a double, a decimal, or even a function. How you handle it
in your application is really an application-specific problem, dependent
upon your application's requirements.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
J

Jon Skeet [C# MVP]

So, a percentage is a rational number, which might be represented by an
integer, a float, a double, a decimal, or even a function. How you handle it
in your application is really an application-specific problem, dependent
upon your application's requirements.

Does a percentage have to be rational, strictly speaking? I can't see
many uses for irrational percentages (pi % for example) but equally I
can't see why it would be theoretically prohibited. It's still a ratio,
but where one or both of the values are irrational.

Of course, it's completely irrelevant to C# :)
 
P

Peter

Kevin said:
Percentage is a way of representing a ratio.
So, a percentage is a rational number, which might be represented by
an integer, a float, a double, a decimal, or even a function. How you
handle it in your application is really an application-specific
problem, dependent upon your application's requirements.

Yes, while I have a need for a "percentage" in my application, what I
really need is a number which can take values from 0 to 100. It is a
configuration setting where the user can set a level from 0% to 100%.

I just wondered if c# had such a possibility in an easy/built-in
fashion.

Although I have not used Pascal in many years I have a hazy
recollection that this was possible via something like specifying
"0..100" in the variable's declaration (but then again I could be
completely wrong).

/Peter

--
 
L

Lasse Vågsæther Karlsen

Peter said:
Yes, while I have a need for a "percentage" in my application, what I
really need is a number which can take values from 0 to 100. It is a
configuration setting where the user can set a level from 0% to 100%.

I just wondered if c# had such a possibility in an easy/built-in
fashion.

Although I have not used Pascal in many years I have a hazy
recollection that this was possible via something like specifying
"0..100" in the variable's declaration (but then again I could be
completely wrong).

/Peter

This was possible in pascal, yes. .NET and C# however does not have this
ability so your only option is pick a data type and check internally,
and then document.
 
P

Peter

Kevin said:
Percentage is a way of representing a ratio.
So, a percentage is a rational number, which might be represented by
an integer, a float, a double, a decimal, or even a function. How you
handle it in your application is really an application-specific
problem, dependent upon your application's requirements.

Yes, while I have a need for a "percentage" in my application, what I
really need is a number which can take values from 0 to 100. It is a
configuration setting where the user can set a level from 0% to 100%.

I just wondered if c# had such a possibility in an easy/built-in
fashion.

Although I have not used Pascal in many years I have a hazy
recollection that this was possible via something like specifying
"0..100" in the variable's declaration (but then again I could be
completely wrong).

/Peter

--
 
K

Kevin Spencer

Hi Jon,

A ratio is not necessarily a rational number, now that you mention it,
since, as you pointed out, pi is a ratio (the ratio of the circumference of
a circle to its diameter). I was thinking about the division aspect of it,
but strictly speaking the numerator and/or the denominator may be any real
number, so a rational number would be a real number.

As for being irrelevant to C#, I don't think so! Although any implementation
must ultimately use integral mathematics, understanding and working with
real numbers is something which some of us tend to run across all too
frequently, and knowing when and how we approximate them may be the
difference between success and failure of our code (ensuring consistency in
results). Also, thinking about the unthinkable is an important aspect to the
progress of knowledge in general, and I personally love to do it.

So, thanks for the correction (again)!

--

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
A

Arne Vajhøj

Peter said:
Yes, while I have a need for a "percentage" in my application, what I
really need is a number which can take values from 0 to 100. It is a
configuration setting where the user can set a level from 0% to 100%.

I just wondered if c# had such a possibility in an easy/built-in
fashion.

Although I have not used Pascal in many years I have a hazy
recollection that this was possible via something like specifying
"0..100" in the variable's declaration (but then again I could be
completely wrong).

You are correct.

Pascal and other languages of that family allows you to specify
types with ranges for better type safety.

Unfortunately (in my opinion) the C family including Java and C#
never implemented that type system.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top