Conversion to double

Dim d As Double
d = CDbl(txtSuppDiscount.Text / 100).

txtCostConverted.Text = (txtUnitCost.Text * d)



  • seancampbellseancampbell Pennsylvania, USA
    Hahaha, the error message was "ITS URGENT"?!

    Consider the data types of the objects you are referencing. If you want to do a math problem, you have to use Number datatypes such as Double, Single, Integer, Long. Lets break down your example and talk about how the system handles this code to help you understand.

    Dim D as Double

    D = CDbl(txtSuppDiscount.Text / 100)
    'CDbl(Object) converts the objects to a Double if it can
    'The CDbl command is only going to process one object, so the
    'code has to resolve txtSuppDiscount.Text / 100 first. So you are
    'doing (String / Integer) first. The system resolves String and
    'tries to convert it to an Integer. If it is a number, it will work
    'fine. String converts to integer... Integer / Integer = Double. Then
    'the CDbl resolves CDbl(Double) after all of that work already happens
    'D = Double

    txtConstConverted.Text = (txtUnitCost.Text * D)
    'In this line, you aren't even trying to convert numberse, you just
    'multiplying a String value with a Double values (String * Double)
    'So the system converts String to Double, if it works great!
    '(Double * Double) = (Double)
    'Now the system has to convert that back to a string datatype
    'so that txtConstConverted.Text = String

    Wow, we can see some crazy data type changing happening there... Lets see what we can do to correct this.

    Firstly, there are ways to check objects if they are Numbers. There is a method called isNumeric() that checks a string if it is a number or not...
    Secondly, anytime we try to do a CType(Object, Type) or CDbl(Object) or CInt(Object) on an object, and the object cannot be converted to that data type, an exception will be thrown and the software will crash. So you either have to use Exception Handling (Try Catch blocks) or check to ensure that the object can be converted with an If statement first...

    Lets explore If statements as a solution
    'txtSuppDiscount.Text needs to be checked if it is a Number
    'txtUnitCost.Text needs to be checked if it is a Number
    Dim D As Double
    If isNumeric(txtSuppDiscount.Text) = False Then
    MessageBox.Show("txtSuppDiscount needs to be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else If isNumeric(txtUnitCost.Text) = False Then
    MessageBox.Show("txtUnitCost needs to be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else 'Both were numbers
    D = CDbl(txtSuppDiscount.Text) / 100.0
    txtCostConverted.Text = (CDbl(txtUnitCost.Text) * D).ToString
    End If
    So, you see I check both textboxes first, and if the first to IF and Else IF conditions return True then I can do my formulas...
    The first line is [Double = CDbl(String) / (Double)]
    The 2nd line [String = (CDbl(String) * Double).ToString]

    In the second line, the string converts to a Double first, then the two doubles resolve the math statement into 1 object, then .ToString tells it it needs to convert to a string (ever so slightly faster then the system having to "figure out" it needs to do that) and then it is assigned to your Text Box's text property.

  • Hi,

    Add [color=Red][b]1 button[/b][/color] and name it [color=Red][b]btnCalculate[/b][/color] and try this code please.>>

    Public Class Form1

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    Dim d As Double = 0
    Double.TryParse(txtSuppDiscount.Text, d)
    d = d / 100

    Dim unitCost As Double = 0
    Double.TryParse(txtUnitCost.Text, unitCost)

    Dim discountedPrice As Double = unitCost - (unitCost * d)
    txtCostConverted.Text = discountedPrice.ToString

    End Sub
    End Class



    Dr M.

  • This post has been deleted.
  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!