Hi i'm having a trouble with this i got 3 textbox and i format it to

Textbox1.Text = Format(Textbox1.Text, "Standard")
Textbox2.Text = Format(Textbox2.Text, "Standard")
Textbox3.Text = Format(Textbox3.Text, "Standard")

which give something like this
1,230.00
2,500.00
1,245.00

and when i add it
Textbox4.text = Val(Textbox1.Text) + Val(Textbox2.Text) + Val(Textbox3.Text)
the result is this
4.00

how can i sum it when the format is in "standard" or "percent"?

Dani AI

Generated

The unexpected result comes from converting the already-formatted text. As pointed out, functions like Val stop at the thousands separator so "1,230.00" becomes 1, "2,500.00" becomes 2, etc. That explains the 4.00 total. was on the right track with parsing — the robust fix is to parse the displayed string with culture-aware number parsing and to do arithmetic on numeric types (Decimal is preferable for money).

A safe, minimal helper (VB.NET) is shown below; it uses a TryParse approach so bad input doesn't throw exceptions and it accepts thousands separators and decimal points according to the current culture:

Imports System.Globalization

Function ParseNumber(text As String) As Decimal
    Dim result As Decimal
    If Decimal.TryParse(text, NumberStyles.Number, CultureInfo.CurrentCulture, result) Then
        Return result
    End If
    Return 0D
End Function

Dim total = ParseNumber(TextBox1.Text) + ParseNumber(TextBox2.Text) + ParseNumber(TextBox3.Text)
TextBox4.Text = total.ToString("N2")

Notes and cautions: for percent-formatted strings parse with NumberStyles.Percent (parsed value is fractional, e.g. "50%" -> 0.5). If your input comes from a fixed culture (for example the UI always shows US formatting), pass a specific CultureInfo instead of CurrentCulture to avoid mis-parses on other machines. Prefer Decimal for currency to avoid floating-point rounding.

Best practice: keep the numeric value separate from the formatted display (store the Decimal in a field or in the control's Tag and only format for display). That makes arithmetic and validation simple and avoids repeatedly parsing user-visible strings.

Recommended Answers

All 3 Replies

Val doesn't recognize a ',' as part of a number and stops converting the string there. Thus you end up with 1+2+1 = 4. Try this instead:
TextBox4.Text = Format((Double.Parse(Textbox1.Text) + Double.Parse(Textbox2.Text) + Double.Parse(Textbox3.Text)), "Standard")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t1 As TextBox = New TextBox
        Dim t2 As TextBox = New TextBox
        Dim t3 As TextBox = New TextBox
        Dim t4 As TextBox = New TextBox
        t1.Text = "1,230.00"
        t2.Text = "2,500.00"
        t3.Text = "1,245.00"
        Dim sum As Decimal = isConvert(t1.Text) + _
                             isConvert(t2.Text) + _
                             isConvert(t3.Text)

        t4.Text = String.Format("{0:N2}", sum)
    End Sub
    Protected Function isConvert(ByVal str As String) As Decimal
        Return Decimal.Parse(str)
    End Function

thank you guys :)

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.