I need to get the sum of two numbers but i do not know how

example

**what the result should be:**
            1,000,000 + 2,000 = 1,002,000

**my code's result:**
            1,000,000 + 2,000 = 3
                     It seems that my code only adds the numbers before comma (,)

My code do not compute it properly. this is my code any way

    lblTotal.Text = Format(Val(lblTotal.Text), "#,##0.00")
    lblVatable.Text = Format(Val(lblVatable.Text), "#,##0.00")
    lblTax.Text = Format(Val(lblTax.Text), "#,##0.00")

Dani AI

Generated

The root cause is the use of Val (it stops at the first non-numeric character, so Val("1,000,000") yields 1). Replacing commas, as suggested, will work in simple cases but is brittle across locales. The better pattern is to keep numeric values in typed variables (as recommended) and parse/format with explicit culture and number styles. Use Decimal for money to avoid floating-point rounding, and choose NumberStyles.Number (or NumberStyles.Currency if currency symbols are expected) with a CultureInfo that matches the input/output.

Imports System.Globalization

Dim a As Decimal
Dim b As Decimal

If Not Decimal.TryParse(lblTotal.Text, NumberStyles.Number, CultureInfo.CurrentCulture, a) Then
    ' handle invalid input
End If

If Not Decimal.TryParse(lblVatable.Text, NumberStyles.Number, CultureInfo.CurrentCulture, b) Then
    ' handle invalid input
End If

Dim sum As Decimal = a + b

lblTotal.Text = a.ToString("N2", CultureInfo.CurrentCulture)
lblVatable.Text = b.ToString("N2", CultureInfo.CurrentCulture)
lblResult.Text = sum.ToString("N2", CultureInfo.CurrentCulture)

Notes: parse and format with the same culture to avoid mismatches; use CultureInfo.InvariantCulture only when input/output is intentionally culture-invariant. For reference see the Decimal.TryParse documentation and the NumberStyles enumeration. ’s invariant-culture idea is valid when all parsing/formatting is controlled; and ’s manual string-splitting approaches are unnecessary if proper parsing is used.

Recommended Answers

All 5 Replies

Use the Replace function to replace the commas with nulls before you do the conversion to int and the addition.

Since your converting the string to Double in order to format it, you could store that Double, and use that in your calculations.

Dim intTotal As Double = Val(lblTotal.Text)
lblTotal.Text = intTotal.ToString("#,###,##0.00")

Dim intVatable As Double = Val(lblVatable.Text)
lblVatable.Text = intVatable.ToString("#,###,##0.00")

Dim intTax As Double = Val(lblTax.Text)
lblTax.Text = intTax.ToString("#,###,##0.00")

If you will manually enter the numbers with the commas you can just simple create the variables that will store the numbers before the commas and also create the variable that will store the commas since it not easy or possible to calculate the numbers with the string format so here is an idea of how you can go about doing it.

Dim TotalNoBforCommaOne As Double
Dim TotalNoBforCommaTwo As Double
Dim TotalNoBforCommaThree As Double
Dim VatableNoBforCommaOne As Double
Dim VatableNoBforCommaTwo As Double
Dim VatableNoBforCommaThree As Double
Dim TaxNoBforCommaOne As Double
Dim TaxNoBforCommaTwo As Double
Dim TaxNoBforCommaThree As Double

' Now you need to track the textbox to detect if the comma has been preesed then
' if the comma is pressed then if it the first time in that textbox then you can
' Assign the desired variable with the current numbers on the textbox then clear the
' textbox so that you can track other entries and assign then with the correct variable.

hmmm how about this??..

Dim n1 as Double = 1000000
Dim n2 as Double = 2000
Dim nTot as Double = n1 + n2

lblTotal.Text = nTot.ToString("#,#", CultureInfo.InvariantCulture)

try dis one.

    Dim total as integer
    dim firstnumber as integer
    dim secondnumber as integer

    total = Format(csng(firstnumber) + Csng(secondnumber),"#,###,##0.00")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.