Hello, everyone, I'm a college student, working on a small project, and I'm having some trouble understanding the Round command on VB. The project is very simple and straight forward, just a simple Bail Bonds program to calculate the bail plus a 10% fee. However, i want to integrate a kind of "promotion", where for every $1000 of the bail cost, $5 would be deducted. I know the formula for this, which is reasonable simple, i just need to know how to round the Bail variable to the nearest 1000 so i can do this calculation. Any suggestions and further help would be much appreciated.

Thank you for your time.

PS: I'm currently running Visual Studio 2008 Professional on Windows 7 Professional. Not sure if that will be significant, but there it is.

## 9 Replies - 14971 Views - Last Post: 18 March 2010 - 01:52 PM

##
**Replies To:** Rounding whole numbers (Integers)

### #2

## Re: Rounding whole numbers (Integers)

Posted 17 March 2010 - 07:54 PM

You'll have to post at least an attempt at code before any is given to you directly - your homework won't be done for you.

But think of it this way: you're trying to figure out how many "thousand"s your number contains. 1031 contains one, 2914 contains two, 7400 contains 7, etc. Think of it in a very simple manner: start at zero, and move up to your number in increments of one thousand using, for example, a For-Next loop.

I'll give another hint: if you do it as I just described, the variable you increment by (typically represented by a letter such as "i"), when divided by one thousand, will give you the "number of thousands" it contains.

But think of it this way: you're trying to figure out how many "thousand"s your number contains. 1031 contains one, 2914 contains two, 7400 contains 7, etc. Think of it in a very simple manner: start at zero, and move up to your number in increments of one thousand using, for example, a For-Next loop.

I'll give another hint: if you do it as I just described, the variable you increment by (typically represented by a letter such as "i"), when divided by one thousand, will give you the "number of thousands" it contains.

**Edit:**Alternatively, you could simply divide your number by 1000, and remove everything after the decimal point... I believe that would work as well.
This post has been edited by **Lumenii**: 17 March 2010 - 07:56 PM

### #3

## Re: Rounding whole numbers (Integers)

Posted 17 March 2010 - 08:51 PM

I thank you for your support, and i apologize for not posting what i had initially thought of. What you described about dividing the number by 1000 was my initial idea for the mathematical formula. The following is what i actually came up with, although I'm having a slight problem, in that if i create the number 10999, it will round it up to 11 after the calculations i performed. Please keep in mind this is still an Intro to VB, so my code will look extremely "noob", I'm sure.

As it is right now, it works just fine, apart from that slight round-up issue. I'm actually surprised it works. Thank you once again, and in advance for any insight.

Admin Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,

PsychoCoder

Public Class Form1 ' Declare constant Const BAIL_FEE_Decimal As Decimal = 0.1D Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click ' Declare Variables Dim BailAmountDecimal, BailFeeDecimal, CollateralValueDecimal, DividedBailAmountDecimal, TotalBailAmountDecimal As Decimal Dim DividedBailAmountInteger, TotalBailDiscountInteger As Integer Try ' Convert Bail Amount to numeric value BailAmountDecimal = Decimal.Parse(BailAmountTextBox.Text) Try ' Convert Collateral Value to numeric value CollateralValueDecimal = Decimal.Parse(CollateralValueTextBox.Text) ' Perform calculations BailFeeDecimal = (BailAmountDecimal * BAIL_FEE_Decimal) DividedBailAmountDecimal = (BailAmountDecimal / 1000) DividedBailAmountInteger = Decimal.Round(DividedBailAmountDecimal, 0) TotalBailDiscountInteger = (DividedBailAmountInteger * 5) TotalBailAmountDecimal = ((BailAmountDecimal + BailFeeDecimal) - TotalBailDiscountInteger) ' Display calculations BailDueTextBox.Text = BailAmountDecimal.ToString("C") BailFeeTextBox.Text = BailFeeDecimal.ToString("C") CollateralInfoTextBox.Text = CollateralTextBox.Text & ", estimated value of " & CollateralValueDecimal.ToString("C") LenniesDiscountTextBox.Text = TotalBailDiscountInteger.ToString("C") TotalTextBox.Text = TotalBailAmountDecimal.ToString("C") Catch CollateralValueException As FormatException MessageBox.Show("Collateral value must be numbers only.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try Catch BailAmountException As FormatException MessageBox.Show("Bail amount must be numbers only.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End Try End Sub End Class

As it is right now, it works just fine, apart from that slight round-up issue. I'm actually surprised it works. Thank you once again, and in advance for any insight.

Admin Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,

PsychoCoder

### #4

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 09:57 AM

Use the integer division operator "\"

14\8 = 1

therefore

11999\1000 = 11. Then multiply by 5 and you get your discount.

14\8 = 1

therefore

11999\1000 = 11. Then multiply by 5 and you get your discount.

### #5

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 09:58 AM

It's "/", not "\".

### #6

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 10:06 AM

### #7

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 11:10 AM

sarmanu, on 18 March 2010 - 08:58 AM, said:

It's "/", not "\".

That's why they call "\" the "integer division operator", to seperate it from "/" regular division. Using the integer division operator there is no rounding required.

11999 / 1000 = 11.999

11999 \ 1000 = 11

Integer division is very handy in cases just like the one in the original question posted by mario_ramalho.

### #8

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 12:36 PM

Thank you all for the feedback. I had intended to use that Integer division, but wasn't sure if it would work as you explained, but i'm going to try it that way. This is a very helpful site, and i really apreciate your time and explanations.

Edit: Thank you, PsycoCoder, for the edit, i shall use the tags from now on. It's a learning process.

Edit: Thank you, PsycoCoder, for the edit, i shall use the tags from now on. It's a learning process.

**Update:**Just switching the division to the Integer division solved the whole problem. It now works perfectly, and i can't thank you guys enough.
This post has been edited by **mario_ramalho**: 18 March 2010 - 12:44 PM

### #9

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 12:46 PM

Damn. I should have not even posted. Gonna go back to C/C++. I don't know anything about VB Sorry for my bad advice.

This post has been edited by **sarmanu**: 18 March 2010 - 12:46 PM

### #10

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 01:52 PM

mario_ramalho, on 18 March 2010 - 11:36 AM, said:

Thank you all for the feedback. I had intended to use that Integer division, but wasn't sure if it would work as you explained, but i'm going to try it that way. This is a very helpful site, and i really apreciate your time and explanations.

Edit: Thank you, PsycoCoder, for the edit, i shall use the tags from now on. It's a learning process.

Edit: Thank you, PsycoCoder, for the edit, i shall use the tags from now on. It's a learning process.

**Update:**Just switching the division to the Integer division solved the whole problem. It now works perfectly, and i can't thank you guys enough.That's how you learn. Try until you run out of options or ideas and then ask questions.

Page 1 of 1