Take care of decimal gaps

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Take care of decimal gaps

    Hi all,
    I have a collection (an array for example) of decimal values.
    I need to round them and take care of the roundings.
    For example, if I have

    decimal a = 10.50m

    should became 10

    and if

    decimal b = 10.51m

    should became 11.

    Then I need to populate a decimal variable with these rest with the sing
    values.
    So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

    In this example

    decimal c = + 0.50 - 0.49 // It will be 0.01

    How can I accomplish this?

    Thanks in advance.
    --
    Luigi

  • Jon Skeet [C# MVP]

    #2
    Re: Take care of decimal gaps

    On Jun 18, 11:16 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
    I have a collection (an array for example) of decimal values.
    I need to round them and take care of the roundings.
    For example, if I have
    >
    decimal a = 10.50m
    should became 10
    and if
    decimal b = 10.51m
    should became 11.
    Have a look at Math.Round(deci mal, MidpointRoundin g)

    Mind you, if you want 9.50m to round down to 9 instead of up to 10, I
    don't think you'll find a version of MidpointRoundin g that does what
    you want.
    Then I need to populate a decimal variable with these rest with the sing
    values.
    So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.
    >
    In this example
    decimal c = + 0.50 - 0.49 // It will be 0.01
    How can I accomplish this?
    That already works. It's not clear to me what you're trying to do
    here.

    Jon

    Comment

    • =?Utf-8?B?THVpZ2k=?=

      #3
      Re: Take care of decimal gaps

      "Jon Skeet [C# MVP]" wrote:
      On Jun 18, 11:16 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
      I have a collection (an array for example) of decimal values.
      I need to round them and take care of the roundings.
      For example, if I have

      decimal a = 10.50m
      should became 10
      and if
      decimal b = 10.51m
      should became 11.
      >
      Have a look at Math.Round(deci mal, MidpointRoundin g)
      >
      Mind you, if you want 9.50m to round down to 9 instead of up to 10, I
      don't think you'll find a version of MidpointRoundin g that does what
      you want.
      >
      Then I need to populate a decimal variable with these rest with the sing
      values.
      So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

      In this example
      decimal c = + 0.50 - 0.49 // It will be 0.01
      How can I accomplish this?
      >
      That already works. It's not clear to me what you're trying to do
      here.
      Hi Jon,
      thanks for reply.
      Mi task is to "not lose" the decimal rounding of these values.
      Ok for the Math.Round it works fine for me, but I've not found a solution to
      recover the decimal values (they are monetary issues).

      Luigi

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Take care of decimal gaps

        On Jun 18, 11:57 am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
        Mi task is to "not lose" the decimal rounding of these values.
        It's not clear to me exactly what you mean.
        Ok for the Math.Round it works fine for me, but I've not found a solutionto
        recover the decimal values (they are monetary issues).
        Recover them from where? What's losing them? Where are you actually
        having problems?

        Jon

        Comment

        • Hans Kesting

          #5
          Re: Take care of decimal gaps

          Luigi has brought this to us :
          Hi all,
          I have a collection (an array for example) of decimal values.
          I need to round them and take care of the roundings.
          For example, if I have
          >
          decimal a = 10.50m
          >
          should became 10
          >
          and if
          >
          decimal b = 10.51m
          >
          should became 11.
          >
          Then I need to populate a decimal variable with these rest with the sing
          values.
          So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.
          >
          In this example
          >
          decimal c = + 0.50 - 0.49 // It will be 0.01
          >
          How can I accomplish this?
          >
          Thanks in advance.
          I'm guessing that you want to round a monetary value to whole (say)
          euro's and keep the "rounding offset"?

          What about:

          decimal originalAmount = 10.50m;
          decimal wholeEuros = Math.Round(orig inalAmount);
          decimal centsFraction = originalAmount - wholeEuros;

          Hans Kesting


          Comment

          • Ben Voigt [C++ MVP]

            #6
            Re: Take care of decimal gaps

            Luigi wrote:
            Hi all,
            I have a collection (an array for example) of decimal values.
            I need to round them and take care of the roundings.
            For example, if I have
            >
            decimal a = 10.50m
            >
            should became 10
            >
            and if
            >
            decimal b = 10.51m
            >
            should became 11.
            >
            Then I need to populate a decimal variable with these rest with the
            sing values.
            So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.
            >
            In this example
            >
            decimal c = + 0.50 - 0.49 // It will be 0.01
            >
            How can I accomplish this?
            IEnumerable<dec imalinput;
            List<decimalout put = new List(decimal);
            decimal error = 0;
            foreach (decimal x in input)
            {
            decimal y = Math.Round(x);
            output.Add(y);
            error += x - y;
            }
            >
            Thanks in advance.

            Comment

            Working...