# Rounding whole numbers (Integers)

Page 1 of 1

## 9 Replies - 16733 Views - Last Post: 18 March 2010 - 01:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=162639&amp;s=9f35870c4bc3def55826721eaa9a7619&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mario_ramalho

Reputation: 0
• Posts: 3
• Joined: 17-March 10

# Rounding whole numbers (Integers)

Posted 17 March 2010 - 07:28 PM

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.

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

Is This A Good Question/Topic? 0

## Replies To: Rounding whole numbers (Integers)

### #2 Lumenii

Reputation: 10
• Posts: 92
• Joined: 04-January 07

## 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.

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 mario_ramalho

Reputation: 0
• Posts: 3
• Joined: 17-March 10

## 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.

```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 ZRonZ

Reputation: 28
• Posts: 206
• Joined: 09-January 09

## 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.

### #5 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 09:58 AM

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

### #6 CharlieMay

• This space intentionally left blank

Reputation: 1727
• Posts: 5,706
• Joined: 25-September 09

## Re: Rounding whole numbers (Integers)

Posted 18 March 2010 - 10:06 AM

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

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

Not when you're dividing to return an integer result.

### #7 ZRonZ

Reputation: 28
• Posts: 206
• Joined: 09-January 09

## 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 mario_ramalho

Reputation: 0
• Posts: 3
• Joined: 17-March 10

## 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.

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 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## 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 ZRonZ

Reputation: 28
• Posts: 206
• Joined: 09-January 09

## 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.

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.