9 Replies  13413 Views  Last Post: 18 March 2010  01:52 PM
#1
Rounding whole numbers (Integers)
Posted 17 March 2010  07:28 PM
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.
Replies To: Rounding whole numbers (Integers)
#2
Re: Rounding whole numbers (Integers)
Posted 17 March 2010  07:54 PM
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 ForNext 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
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 roundup 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
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
#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:
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
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
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:
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.
