Results 1 to 3 of 3

Thread: Rounding up problem vb.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    22

    Question Rounding up problem vb.net

    Ive been searching the forum for an easy way to round numbers but i couldnt find what i was looking for.
    lets say if i have a variable and i want to round it up to the closest number so that for example 1.02 = 2, 3.6 = 4, 1.5=2 etc

    is there any easy way of doing this without lots of code?
    cant really get the round function to work the way i want, i must be doing something wrong and when i try to fix it - it justresults in more and more code. There must be a really easy way of doing this since it is a quite common thing you need?

    when answering prtend your helping a 5 year old since im really new to programming and vs 2008.

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Rounding up problem vb.net

    If you always want to round up to the nearest integer, then use something like this:

    Code:
    Dim d As Decimal = 1.02
    Dim i As Integer = Math.Ceiling(d)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Rounding up problem vb.net

    Just note that Math.Ceiling rounds toward positive infinity, so positive numbers will round away from zero and negative numbers will round towards zero. If you're not using any negative numbers or that's what you want then there's no issue, but if you wanted all numbers to round away from zero then you'll have to jiggle it a bit, using Math.Floor for negative numbers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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