|
-
Feb 15th, 2011, 12:49 PM
#1
Thread Starter
Junior Member
Prevent integers from being rounded?
I have two textboxes that will contain numbers. I convert those text boxes to integers and then want to multiply them by certain values and then show the new value in a different textbox. For example, 100 gets converted to an integer and then multiplied by 1.75 and the value displayed in the other textbox is 200 instead of 175. How do I make it so the value isn't rounded to the nearest 100?
Here is my code, compTotalPremium.Text and collTotalPremium.text are the boxes that are showing the rounded value.
Code:
Dim Comp As Integer = compDedPremium.Text
Dim Coll As Integer = collDedPremium.Text
Dim CompMulti As Integer = compDedMulti.Text
Dim CollMulti As Integer = collDedMulti.Text
compTotalPremium.Text = (Comp * CompMulti)
collTotalPremium.Text = (Coll * CollMulti)
Last edited by Boots13; Feb 15th, 2011 at 12:53 PM.
-
Feb 15th, 2011, 01:06 PM
#2
Re: Prevent integers from being rounded?
What did you expect the answer to be? the result will take on the type of the left hand operand... in this case 100, so it takes the 1.75 and implicitly converts it to an integer... 2... 100 * 2 = 200... if you want any thing else, you need to reverse your operands... make it 1.75 * 100 .... or do an explicit conversion of your 100 into a decimal....100d * 1.75 = 175...
EDIT.... actually what I wrote isn't correct at all... you do know and understand that integers are WHOLE NUMBERS, right? 1.75 is NOT a valid integer... so when you assign 1.75 to an integer varaible.... yeah, it's going to round to the nearest integer... 2 in this case...
If you're dealing with decimals and want to keep the precision then you need to use the appropriate type... probably Decimal.
Also, it's clear you don't have Option Strict turned on ... which you really should. You'll then need to change your code to do explicit conversion from string (.text) to the appropriate data type.
-tg
Last edited by techgnome; Feb 15th, 2011 at 01:09 PM.
-
Feb 15th, 2011, 01:06 PM
#3
Re: Prevent integers from being rounded?
Integer means whole number. You declared you multiplier as an integer so when you tried to convert "1.75" to an integer, it will round the value up to 2.
Bottom line: you have to declare you multiplier as a floating point variable such as Single, Double or Decimal.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 15th, 2011, 01:21 PM
#4
Thread Starter
Junior Member
Re: Prevent integers from being rounded?
That helps out a bunch and makes complete sense. I tried changing it to Double and it worked perfect. Thanks for the help guys.
-
Feb 15th, 2011, 03:22 PM
#5
Re: Prevent integers from being rounded?
You might want to consider using Decimal rather than Double. It may not matter, depending on the actual values you are using, but it seems likely that you will eventually end up with a value that will be off by a slight amount. That would make your result x.0000000001 or x.999999999999, which is a bit ugly. The reason has to do with binary floating point numbers not being able to represent some decimals precisely. The Decimal type avoids that problem, though it will be somewhat slower.
The alternative would be to use one of the formats for .ToString. Since you should have Option Strict ON, you will need to convert the number to a string to put into the textbox, and .ToString has a load of options for formatting the result.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|