Specify precision of decimal?

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

    #1

    Specify precision of decimal?

    I have a decimal variable. I'd like it to store a number rounded to
    two decimal places.

    I've declared and loaded it like this:

    CODE
    private Decimal FileSize_MB_Dat aFile = 0.0M;
    this.FileSize_M B_InitialFile = Convert.ToDecim al(fileInfo.Len gth) /
    1048576; //1024 x 1024

    I want the actual value of the variable to be, for example, .75 or
    15.28 or whatever (I don't want to convert it to a string and format
    it unless that's strictly necessary). When I look at the value of the
    variable in the debugger it goes out to like 15 decimal places. I'm
    sure this is quite simple.

    I realize I could keep the precision but these values are going into a
    database and I don't want/need the extra digits.

    Thanks.
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Specify precision of decimal?

    On Apr 1, 11:19 am, Anodes <jjs...@goowy.c omwrote:
    I have a decimal variable. I'd like it to store a number rounded to
    two decimal places.
    >
    I've declared and loaded it like this:
    >
    CODE
    private Decimal FileSize_MB_Dat aFile = 0.0M;
    this.FileSize_M B_InitialFile = Convert.ToDecim al(fileInfo.Len gth) /
    1048576; //1024 x 1024
    >
    I want the actual value of the variable to be, for example, .75 or
    15.28 or whatever (I don't want to convert it to a string and format
    it unless that's strictly necessary). When I look at the value of the
    variable in the debugger it goes out to like 15 decimal places. I'm
    sure this is quite simple.
    >
    I realize I could keep the precision but these values are going into a
    database and I don't want/need the extra digits.
    >
    Thanks.
    Hi,

    I think it's a bad idea doing that. you can display the value with two
    decimal places but you should keep as many as it allows. Otherwise you
    can get into rounding problems when you operate in these values.

    Comment

    • Gregg Walker

      #3
      Re: Specify precision of decimal?

      Sure, but I'd like to specify it in code. I'm going with this:
      this.FileSize_M B_InitialFile =
      Math.Round(Conv ert.ToDecimal(f ileInfo.Length) /
      1048576), 2);
      Caveat emptor with Math.Round ...

      Be sure you understand how MidpointRoundin g is implemented in the default
      overloads. I ended up with a "wrong" rounding for many calculations because
      the default MidpointRoundin g (ToEven) uses "bankers rounding" which is not
      used in our industry. We use MidpointRoundin g.AwayFromZero which is what I
      learned in high school and have seen used every where I have worked.
      --
      Gregg Walker


      Comment

      • Gregg Walker

        #4
        Re: Specify precision of decimal?

        I think it's a bad idea doing that. you can display the value with two
        decimal places but you should keep as many as it allows. Otherwise you
        can get into rounding problems when you operate in these values.
        Ignacio -- I have to respectfully disagree with what you stated.

        IMHO rounding needs to be applied precisely when business logic dictates
        that it be applied. Not before and not after. Applying rounding at the
        wrong time (calculation) is what leads to the future rounding problems as
        you suggested.

        If Anodes business logic requires that he store a value with 2 decimal
        places in the database then rounding before the database operation takes
        place is the appropriate time. If not then not.
        --
        Gregg Walker


        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: Specify precision of decimal?

          Ignacio Machin ( .NET/ C# MVP ) wrote:
          On Apr 1, 2:00 pm, "Gregg Walker" <xy...@newsgrou p.nospamwrote:
          >>I think it's a bad idea doing that. you can display the value with two
          >>decimal places but you should keep as many as it allows. Otherwise you
          >>can get into rounding problems when you operate in these values.
          >Ignacio -- I have to respectfully disagree with what you stated.
          >>
          >IMHO rounding needs to be applied precisely when business logic dictates
          >that it be applied. Not before and not after. Applying rounding at the
          >wrong time (calculation) is what leads to the future rounding problems as
          >you suggested.
          >
          I think I suggested exactly the opposite, do not do rounding in the
          calculations (or in the storage of the values) only when you need to
          display the value you should roundit.
          Agree that the above is the general case, it might be cases where it
          does not apply, IMHO those are the few.
          Decimal is mostly used for money.

          My experience is the same as Gregg's: the business rules determine
          when rounding should happen and it is not necessarily latest moment.
          Rounding twice at different places in the calculations are also
          sometimes seen.

          For float and decimal I completely agree with you - round when
          outputting. But accountants think differently.

          Arne

          Comment

          Working...