Using Rounding on a Double

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

    #1

    Using Rounding on a Double

    Hi

    I have a double that i want to round to a certain number of decimal places
    this is the code i use

    Return Math.Round(mdNu mber, mbDecimalPlaces )

    mbDecimalPlaces is the number of decimal places that i want, but it doesn't
    use them with two decimal places i still get 0.0 or 9.9

    Any ideas?
    --


    Kind Regards

    Danny Woolston




  • Jonathan Amend

    #2
    Re: Using Rounding on a Double

    You could use the ([number]).ToString([number formatting expression]) like
    so:

    Dim TestNumber as Double = 1.2345
    Dim strNumber as String
    strNumber = TestNumber.ToSt ring("#.00000")
    'strNumber = "1.23450"
    strNumber = TestNumber.ToSt ring("#.00")
    'strNumber = "1.23"
    strNumber = TestNumber.ToSt ring("00.#")
    'strNumber = "01.2345"

    "Jon Skeet" <skeet@pobox.co m> wrote in message
    news:MPG.197f76 076af6f8a298a11 7@news.microsof t.com...[color=blue]
    > Danny Woolston <daniel.woolsto n@farmade.com> wrote:[color=green]
    > > I have a property of a control which will return a double, but the[/color][/color]
    control[color=blue][color=green]
    > > allows the user to return a number of decimal places.
    > >
    > > So if the decimal places are set to 2 and the number stored is 1.021[/color][/color]
    then on[color=blue][color=green]
    > > a call to the number property i want returned 1.02, or if the the number
    > > stored is 1.3 and a decimal place of 3 then i want returned 1.300.
    > >
    > > Let me stress that this is number only property i cannot use the format
    > > property of the string.[/color]
    >
    > Well you've got to - because doubles themselves don't *have* a number
    > of decimal places, they just have values. You can round a double to the
    > extent that rounding 1.021 to 2DP will round it to the closest double
    > value to 1.02, but there's no difference in the double itself between
    > 1.5 and 1.50.
    >
    > --
    > Jon Skeet - <skeet@pobox.co m>
    > http://www.pobox.com/~skeet/
    > If replying to the group, please do not mail me too[/color]


    Comment

    • Danny Woolston

      #3
      Re: Using Rounding on a Double

      Hi

      Thanks for the reply, but i need the number as a double not a string

      --


      Kind Regards

      Danny Woolston
      Software Developer



      "Jonathan Amend" <cephas_is@hotm ail.com> wrote in message
      news:3f15f0db$1 _2@aeinews....[color=blue]
      > You could use the ([number]).ToString([number formatting expression]) like
      > so:
      >
      > Dim TestNumber as Double = 1.2345
      > Dim strNumber as String
      > strNumber = TestNumber.ToSt ring("#.00000")
      > 'strNumber = "1.23450"
      > strNumber = TestNumber.ToSt ring("#.00")
      > 'strNumber = "1.23"
      > strNumber = TestNumber.ToSt ring("00.#")
      > 'strNumber = "01.2345"
      >
      > "Jon Skeet" <skeet@pobox.co m> wrote in message
      > news:MPG.197f76 076af6f8a298a11 7@news.microsof t.com...[color=green]
      > > Danny Woolston <daniel.woolsto n@farmade.com> wrote:[color=darkred]
      > > > I have a property of a control which will return a double, but the[/color][/color]
      > control[color=green][color=darkred]
      > > > allows the user to return a number of decimal places.
      > > >
      > > > So if the decimal places are set to 2 and the number stored is 1.021[/color][/color]
      > then on[color=green][color=darkred]
      > > > a call to the number property i want returned 1.02, or if the the[/color][/color][/color]
      number[color=blue][color=green][color=darkred]
      > > > stored is 1.3 and a decimal place of 3 then i want returned 1.300.
      > > >
      > > > Let me stress that this is number only property i cannot use the[/color][/color][/color]
      format[color=blue][color=green][color=darkred]
      > > > property of the string.[/color]
      > >
      > > Well you've got to - because doubles themselves don't *have* a number
      > > of decimal places, they just have values. You can round a double to the
      > > extent that rounding 1.021 to 2DP will round it to the closest double
      > > value to 1.02, but there's no difference in the double itself between
      > > 1.5 and 1.50.
      > >
      > > --
      > > Jon Skeet - <skeet@pobox.co m>
      > > http://www.pobox.com/~skeet/
      > > If replying to the group, please do not mail me too[/color]
      >
      >[/color]


      Comment

      • Anthony Moore

        #4
        Re: Using Rounding on a Double

        If you have .Net V1.1 you can try using the Decimal class. Unlike Double,
        it does consider trailing decimal places as significant. It can also store
        more data than Double and the precision for decimal number does not drop
        off. However, it is bigger and slower than Double (16 bytes vs 8 bytes).

        V1.0 did not support this feature on Double. It was changed to comply with
        the new ECMA standard.

        Comment

        • Anthony Moore

          #5
          Re: Using Rounding on a Double

          If you have .Net V1.1 you can try using the Decimal class. Unlike Double,
          it does consider trailing decimal places as significant. It can also store
          more data than Double and the precision for decimal number does not drop
          off. However, it is bigger and slower than Double (16 bytes vs 8 bytes).

          V1.0 did not support this feature on Double. It was changed to comply with
          the new ECMA standard.

          Comment

          • Anthony Moore

            #6
            Re: Using Rounding on a Double

            If you have .Net V1.1 you can try using the Decimal class. Unlike Double,
            it does consider trailing decimal places as significant. It can also store
            more data than Double and the precision for decimal number does not drop
            off. However, it is bigger and slower than Double (16 bytes vs 8 bytes).

            V1.0 did not support this feature on Double. It was changed to comply with
            the new ECMA standard.

            Comment

            Working...