Results 1 to 5 of 5

Thread: Prevent integers from being rounded?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    28

    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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    28

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width