percentage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    percentage

    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

    --

  • Peter Duniho

    #2
    Re: percentage

    On Tue, 19 Feb 2008 00:20:45 -0800, Peter <xdzgor@hotmail .comwrote:
    [...]
    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 ArgumentExcepti on, or handle the error some other way if
    you don't think that's appropriate.

    Pete

    Comment

    • Peter

      #3
      Re: percentage

      Kevin Spencer wrote:
      Percentage is a way of representing a ratio.
      <snip>
      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

      --

      Comment

      • Peter

        #4
        Re: percentage

        Kevin Spencer wrote:
        Percentage is a way of representing a ratio.
        <snip>
        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

        --

        Comment

        • =?UTF-8?B?QXJuZSBWYWpow7hq?=

          #5
          Re: percentage

          Peter wrote:
          Kevin Spencer wrote:
          >Percentage is a way of representing a ratio.
          <snip>
          >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).
          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

          Comment

          Working...