Results 1 to 15 of 15

Thread: Rounding value

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Question Rounding value

    Hello, m8
    I want to know what is the equation which round 7.33 to 7.35 or 7.67 to 7.70
    meaning round the 2nd decimal number to the nearest five
    Thank you

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

    Re: Rounding value

    Firstly, that isn't rounding the second decimal number to the nearest five or else 7.67 would become 7.65. So, do you actually want to round or specifically round up?

    Either way, there's no one method to that exactly. You would have to multiply, then round or ceiling, then divide. Exactly how depends on whether you want to round or ceiling.
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by jmcilhinney View Post
    Firstly, that isn't rounding the second decimal number to the nearest five or else 7.67 would become 7.65. So, do you actually want to round or specifically round up?

    Either way, there's no one method to that exactly. You would have to multiply, then round or ceiling, then divide. Exactly how depends on whether you want to round or ceiling.
    Thank you for your reply
    I want to round up whatever the value of the second decimal number to nearest five (7.01 to 7.05 or 7.333 to be 7.35 and 7.61 to be 7.65) like that not to the least value.
    in EXCEL the CEILING function is doing that by Ceiling(cell number , 0.05)
    Name:  excel.png
Views: 227
Size:  5.1 KB
    but how can I do it in VB.net
    Last edited by mohammedkhairy; Jan 17th, 2016 at 12:33 AM.

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

    Re: Rounding value

    In VB.NET you would use the Math.Ceiling method. It only rounds to whole numbers though, so you'll need to multiply first and then divide afterwards.
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by jmcilhinney View Post
    In VB.NET you would use the Math.Ceiling method. It only rounds to whole numbers though, so you'll need to multiply first and then divide afterwards.
    Can you please do an example showing the method

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

    Re: Rounding value

    Quote Originally Posted by mohammedkhairy View Post
    Can you please do an example showing the method
    Why can't you make an effort to do it for yourself? I've told you what to do. Just do it.
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by jmcilhinney View Post
    Why can't you make an effort to do it for yourself? I've told you what to do. Just do it.
    Thank you, for our effort
    but I'm new using VB.NET, what I understand from your answer that I should use MATH.CEILING, which will round the positive value to the next bigger integer number " 7.333 will be 8" .
    But I don't understand what do you mean by
    Quote Originally Posted by jmcilhinney View Post
    you'll need to multiply first and then divide afterwards

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Rounding value

    As an example:
    If you multiply 7.333 by 100 it becomes 733.3
    The Ceiling of that is 734
    Divide that by the same as you multiplied by (in this case 100), and the result is 7.34

    What you need to do is work out what number to multiply/divide by.

    As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by si_the_geek View Post
    As an example:
    If you multiply 7.333 by 100 it becomes 733.3
    The Ceiling of that is 734
    Divide that by the same as you multiplied by (in this case 100), and the result is 7.34

    What you need to do is work out what number to multiply/divide by.

    As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.
    Thank you,
    But it will only round to the bigger value 734 or 761 I need it to be 735 or 765 (to the nearest five)

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Rounding value

    If you multiply (and divide) by 100, then yes... but that is why I wrote the second half of my post:
    Quote Originally Posted by si_the_geek View Post
    What you need to do is work out what number to multiply/divide by.

    As with many programming problems, this kind of thing is not specific to VB.Net or even to programming.
    While we could do it all for you, that would mean you are less likely to learn/understand.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by si_the_geek View Post
    If you multiply (and divide) by 100, then yes... but that is why I wrote the second half of my post:


    While we could do it all for you, that would mean you are less likely to learn/understand.
    Ok, Thank you
    I will try and l hope solve this problem
    Thanks for your concern again

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

    Re: Rounding value

    IF you are going UP to the next .05 ... which isn't rounding... try this:
    Code:
    	Public Sub Main()
    		dim someNum as Decimal =7.5151 '5.01
    		
    		Console.WriteLine(someNum.ToString())
    		someNum *= 100 '751.51
    		Console.WriteLine(someNum.ToString())
    		someNum = Math.Ceiling(someNum) '752\
    		Console.WriteLine(someNum.ToString())
    		someNum += (5- ((someNum + 5) mod 5)) '<--- this is the key here
    		
    		'Console.WriteLine((5-((someNum + 5) mod 5)).ToString)
    		
    		Console.WriteLine(someNum.ToString())
    		someNum /= 100		
    		Console.WriteLine(someNum.ToString())
    		
    	End Sub
    https://dotnetfiddle.net/06Vzyf

    PArt of the issue is you keep calling it rounding, but that isn't what you're looking for... at any rate, this gives you the results you defined with your excel screen shot. There is a little more to it than just the multiply/divide ... you also need to figure how far off from the next 5 you are. I've done this by adding 5, using mod to get the remainder, then subtracting that from 5 and adding it back to the original number. There might be a way to simplify it.

    Using the above, I got 7.55 and 5.05 for the final results.

    -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??? *

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Rounding value

    There's no need for Mod etc, just picking the right number (which is less than 100, and is the reciprocal of the 'rounding' amount) is enough.

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: Rounding value

    Quote Originally Posted by techgnome View Post
    IF you are going UP to the next .05 ... which isn't rounding... try this:
    Code:
    	Public Sub Main()
    		dim someNum as Decimal =7.5151 '5.01
    		
    		Console.WriteLine(someNum.ToString())
    		someNum *= 100 '751.51
    		Console.WriteLine(someNum.ToString())
    		someNum = Math.Ceiling(someNum) '752\
    		Console.WriteLine(someNum.ToString())
    		someNum += (5- ((someNum + 5) mod 5)) '<--- this is the key here
    		
    		'Console.WriteLine((5-((someNum + 5) mod 5)).ToString)
    		
    		Console.WriteLine(someNum.ToString())
    		someNum /= 100		
    		Console.WriteLine(someNum.ToString())
    		
    	End Sub
    https://dotnetfiddle.net/06Vzyf

    PArt of the issue is you keep calling it rounding, but that isn't what you're looking for... at any rate, this gives you the results you defined with your excel screen shot. There is a little more to it than just the multiply/divide ... you also need to figure how far off from the next 5 you are. I've done this by adding 5, using mod to get the remainder, then subtracting that from 5 and adding it back to the original number. There might be a way to simplify it.

    Using the above, I got 7.55 and 5.05 for the final results.

    -tg
    Thank you very much, your answer is very clear and I understand it

    Quote Originally Posted by si_the_geek View Post
    There's no need for Mod etc, just picking the right number (which is less than 100, and is the reciprocal of the 'rounding' amount) is enough.
    Thanks, I multiply the value with 20 and ceiling it then divided it by 20 and get the answer
    Example Code:
    1. Dim x as decimal
    2. x = math.ceiling(7.33333*20)
    3. textbox?.text = x/20
    4. ' 7.33 >> 7.35

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Rounding value

    That's it, well done

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