Results 1 to 4 of 4

Thread: Math.Round / Math.Ceiling

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    180

    Math.Round / Math.Ceiling

    Hi,

    I need my function to round an incoming decimal up to the next decimal.

    For 100.99 I need to return 101.00
    For 95.45 I need to return 96.00
    For 50.00 I need to return 51.00

    I've tried math.round and math.ceiling but there are situations when neither work. Any other ideas?

    Thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Math.Round / Math.Ceiling

    So it looks like you always need the returned value to be 1 higher on the integral part of the number, eventhough the decimal part is zero. Is that correct? If so, try this
    Code:
            Dim num As Double = 12.0F
            Dim newNum As Double = Math.Truncate(num) + 1.0F
            MessageBox.Show(newNum.ToString)

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Math.Round / Math.Ceiling

    Quote Originally Posted by MondeoST24
    For 100.99 I need to return 101.00
    Math.Ceiling
    Quote Originally Posted by MondeoST24
    For 95.45 I need to return 96.00
    Math.Ceiling
    Quote Originally Posted by MondeoST24
    For 50.00 I need to return 51.00
    Eh? What kind of rounding is that? Doesn't make sense.

    stanav probably has the closest thing to it, but I'd use Math.Floor rather than .Truncate, then add 1 to it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Math.Round / Math.Ceiling

    Code:
            If (n Mod Math.Floor(n) = 0) Then
                m = n + 1
            Else
                m = Math.Ceiling(n)
            End If

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