Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2004, 11:03 PM
Authorized User
 
Join Date: Aug 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rounding Numbers

Hi. I'm having a slight problem with VB's automatically rounding numbers. Take the following scenario:

Dim number1 As Integer = 90
Dim number2 As Integer = 46
Dim number3 As Integer = (number2/number1)

What I want is number3 returned as a whole number. However, in the above example VB rounds number3 up to 1. I want to keep it 0 since 46 did not go into 90 1 time. I hope I've been clear.

Thanks in advance to anyone who spends time reading this and/or helping.

 
Old February 2nd, 2004, 11:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

46/90 is 0.51 and will therefore be rounded up to 1.
45/90 will be rounded down to 0.

The Int() and Fix() will return the result you are after but they behave differently with negative numbers, so check them out if you need to use negative numbers.
number3 = Int(number2/number1)

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 3rd, 2004, 06:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

VB has an Integer division operator (\ not /) which does what you want.
46\90 => 0

hth
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding Numbers and Displaying Decimals elygp Crystal Reports 2 May 8th, 2007 10:56 AM
Rounding Numbers ricespn Beginning VB 6 5 November 6th, 2006 05:24 PM
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
What's with the rounding?? kaizer BOOK: Beginning Java 2 2 December 22nd, 2003 11:36 PM
Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.