Rounding decimal

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

    #1

    Rounding decimal

    Hi all,
    I have to write a snippet that performs a rounding of decimal value, follow
    this rules:

    1.49m -1m
    1.5 -2m
    1.51 -2m

    -1.49 --1m
    -1.50 --2

    I was trying this:

    Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

    but not works correctly.

    Has anyone idea?

    Thanks a lot.
    --
    Luigi

  • Hans Kesting

    #2
    Re: Rounding decimal

    After serious thinking Luigi wrote :
    Hi all,
    I have to write a snippet that performs a rounding of decimal value, follow
    this rules:
    >
    1.49m -1m
    1.5 -2m
    1.51 -2m
    >
    -1.49 --1m
    -1.50 --2
    >
    I was trying this:
    >
    Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);
    >
    but not works correctly.
    >
    Has anyone idea?
    >
    Thanks a lot.
    Math.Round has an overload that accepts a MidpointRoundin g value. You
    can set that to "ToEven" or "AwayFromZe ro". I guess you need the second
    value.

    Hans Kesting


    Comment

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

      #3
      RE: Rounding decimal

      A small correction for negative values.
      These are the rules:

      -1.5 = - 1

      - 1.6 = - 2

      -1.4 = - 1


      L

      Comment

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

        #4
        Re: Rounding decimal

        "Hans Kesting" wrote:
        After serious thinking Luigi wrote :
        Hi all,
        I have to write a snippet that performs a rounding of decimal value, follow
        this rules:

        1.49m -1m
        1.5 -2m
        1.51 -2m

        -1.49 --1m
        -1.50 --2

        I was trying this:

        Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

        but not works correctly.

        Has anyone idea?

        Thanks a lot.
        >
        Math.Round has an overload that accepts a MidpointRoundin g value. You
        can set that to "ToEven" or "AwayFromZe ro". I guess you need the second
        value.
        Ok Hans, I'll checking.

        Luigi

        Comment

        Working...