Results 1 to 15 of 15

Thread: [RESOLVED] Rounding Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    35

    Resolved [RESOLVED] Rounding Problem

    I have some calculated values like 5823, 7459.2 and i want to display it as
    5820 ,7460 like that. i am display this values in data grid.

    Please help in this.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rounding Problem

    Have you taken a look at VB6's ROUND function?

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Rounding Problem

    There are no built-in functions that will round 5823 down to 5820 or 7459.2 up to 7460 - anyone who's more or less familiar with elementary math knows why.
    You will have to write your own function to do that. I think we have some samples posted so try searching.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    35

    Re: Rounding Problem

    Thanks for the Reply..
    I have used round function but it will not help me in this case

    Rhinobull do you have any function or any thread you know than please tell me.

  5. #5
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Rounding Problem

    If you divide by ten, take the integer/long then multiply by ten it should give you a number always ending in zero.

    Cadman

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    35

    Re: Rounding Problem

    hi cadman
    i have tried what u have told me but it was not working it will not give me values in zero

  8. #8
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Rounding Problem

    What is your data type? Integers and longs should strip decimals out.
    You may need to copy them into an integer or long first instead of single or double precision then do the divide, multiply as I suggested above.
    So for a number such as 7459.2 as an integer would be 7459. Divide by ten would be 745. Mult by ten gives 7450.
    Also the Fix function could be used to strip decimals like:
    Fix(7459.2/10)*10 would give 7450.

  9. #9
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Rounding Problem

    Oops, you wanted 7560 not 7450.
    7459.2
    Try: round(7459.2/10)*10

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Rounding Problem

    Code:
    Private Sub Form_Load()
    
    Dim lngValue As Long
    
    lngValue = 5823 '7459.2
    
    lngValue = Round(lngValue, 0)
    If Right$(lngValue, 1) < 5 Then
        MsgBox SymDown(lngValue, 0.1)
    Else
        MsgBox SymUp(lngValue, 0.1)
    End If
    
    End Sub
    Function SymDown(ByVal X As Double, _
             Optional ByVal Factor As Double = 1) As Double
      SymDown = Fix(X * Factor) / Factor
    End Function
    
    Function SymUp(ByVal X As Double, _
             Optional ByVal Factor As Double = 1) As Double
    Dim Temp As Double
      Temp = Fix(X * Factor)
      SymUp = (Temp + IIf(X = Temp, 0, Sgn(X))) / Factor
    End Function

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Rounding Problem

    Two way of rounding you can choose:
    Code:
        '-- banker rounding
        n = CLng(x / 10) * 10 
    
        '-- standard rounding: 0 to 4 round down, 5 to 9 round up
        n = Int((x + 5) / 10) * 10
        '-- or
        n = ((x + 5) \ 10) * 10
    Code:
        x1 = 5823
        x2 = 5825
        x3 = 5835
        x4 = 7459.2
        
        '-- banker rounding
        n1 = CLng(x1 / 10) * 10 '-- gives 5823
        Debug.Print x1, n1
        n2 = CLng(x2 / 10) * 10 '-- gives 7460
        Debug.Print x2, n2
        n3 = CLng(x3 / 10) * 10 '-- gives 5820
        Debug.Print x3, n3
        n4 = CLng(x4 / 10) * 10 '-- gives 5840
        Debug.Print x4, n4
        Debug.Print
    
        '-- standard rounding: 0 to 4 round down, 5 to 9 round up
        n1 = Int((x1 + 5) / 10) * 10 '-- gives 5823
        Debug.Print x1, n1
        n2 = Int((x2 + 5) / 10) * 10 '-- gives 7460
        Debug.Print x2, n2
        n3 = Int((x3 + 5) / 10) * 10 '-- gives 5830
        Debug.Print x3, n3
        n4 = Int((x4 + 5) / 10) * 10 '-- gives 5840
        Debug.Print x4, n4
        Debug.Print
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Rounding Problem

    Just take advantage of Visual Basics rounding...

    It can in fact round.

    Anything declated as an integer will automatically round for you. So...

    vb Code:
    1. Dim X as integer
    2. x = The number you need rounded

    Thats all

    EDIT: whoops misread your starting post. This function will only work for rounding decimals.
    Last edited by Alvanian; Sep 9th, 2008 at 09:31 PM.

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Rounding Problem

    Quote Originally Posted by Alvanian
    Just take advantage of Visual Basics rounding...

    It can in fact round.

    Anything declated as an integer will automatically round for you. So...

    vb Code:
    1. Dim X as integer
    2. x = The number you need rounded

    Thats all
    Round to what? Reread the requirement.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Rounding Problem

    And you didnt read my post all the way through.
    That only rounds decimal points. Even then thats still half the problem. From there you can just get the last digit in the number. Subtract it from 9 if its greater than 4 then add the difference +1 back.

    Or if its less than 5 just subtract it from the whole. ill submit code later got work now

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    35

    Re: Rounding Problem

    Thank You very much anhn your idea works for me
    it gives me perfect values whatever i need
    you are great.............

    Thanks again..........

    Thanks to all for your reply.

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