Results 1 to 6 of 6

Thread: rounding problem

  1. #1

    Thread Starter
    Addicted Member dani2's Avatar
    Join Date
    Feb 2005
    Location
    Sibiu.ro
    Posts
    191

    rounding problem

    Hi,
    Is there a way for rounding a decimal up or down? The vb .net Decimal.Round function always rounds to nearest.

    VB Code:
    1. Decimal.Round(123.456789, 4) = 123.4568
    Home is where your Head is

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: rounding problem

    Just wanna try as no one answers
    VB Code:
    1. double d=double.Parse((123.456789).ToString("###,###0.0000"));

  3. #3
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: rounding problem

    Just off the top of my head (actually pretty barren up there)
    I think if you add or subtract 1/2 of the lowest digit's value, i.e. 0.5, you can force the round to round up/down.

    e.g. to force 123.xxxxxxx to round UP to 4 decimal places first add 0.00005 before doing the round.
    to force 123.xxxxxxx to round DOWN to 4 decimal places first subtract 0.00005

    For 3 decimal places add/subtract 0.0005.

    No guarantees, but give it a try and see if it does what you want.
    DaveBo out
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: rounding problem

    NOTE: VB.Net does not round integers in the same way that VB6 used to.

    See MSDN for specific functions.
    I don't live here any more.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: rounding problem

    Quote Originally Posted by kulrom
    Perhaps I just haven't read enough this article .. only i found this by MSDN search engine and placed the link ... sorry, my mistake

    If you ignore the code samples, that article still contains relevant information.
    I don't live here any more.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: rounding problem

    Double - again!

    Don't convert a nice precise datatype like DECIMAL to DOUBLE - never - please - never...

    Ok - I feel better now.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width