Friends I have a problem in totalling the variable .When I use my code I am not getting the answer as I expected. My code is as below

Jsharbal = Format$(TRS("Sharbal"), "#,##0.00")
Jworkbal = Format$(TRS("workbal"), "#,##0.00")
Jsalabal = Format$(TRS("Salabal"), "#,##0.00")
Jprofit = Format$(TRS("Prflbal"), "#,##0.00")
CRTOTAL = (Val(Jsharbal.Caption) + Val(Jworkbal.Caption) + Val(Jsalabal.Caption)+ Val(Jprofit.Caption))
LblCrtotal.Caption = Format$(CRTOTAL, "#,##0.00")

the values of my above variables are 900,000.00,4,500.00,30,000.00,10,000.00 When working I am getting the answer as 944.00 Instead of 944,500.00 Why this mistake. and if I am changing the above code like this

Jsharbal = TRS("Sharbal")
Jworkbal = TRS("workbal")
Jsalabal = TRS("Salabal")
Jprofit = TRS("Prflbal")
CRTOTAL = (Val(Jsharbal.Caption) + Val(Jworkbal.Caption) + Val(Jsalabal.Caption)+ Val(Jprofit.Caption))
LblCrtotal.Caption = Format$(CRTOTAL, "#,##0.00")

I am getting the answer as 944,500.00 As I desired.But here the values of the variables are changed like trhis 900000,4500,30000,10000 I want the thoudsand separator in all the values ..Any body help me please

Dani AI

Generated

Short version: the problem was the formatted strings, not the math. Format$ produces strings with commas, and VB's Val stops parsing at the first non-numeric character (a comma). So Val("900,000.00") yields 900, Val("4,500.00") yields 4, etc., which is why the total came out as 944.00. Moving formatting to after the calculation, as did, is the right quick fix and matches 's separation-of-concerns advice.

Practical recommendations:

  • Keep values numeric while you calculate. Let TRS return a numeric type (Double, Decimal or Currency) and only Format the final total for display.
  • If you must convert a formatted string back to a number, strip thousands separators first, or use a culture-aware parser.

Examples (not repeating the original code shown above):

' VB6/VBA: remove commas before converting
Dim n As Double
n = Val(Replace(formattedString, ",", ""))
' VB.NET: parse with NumberStyles to allow thousands
Dim result As Double
If Double.TryParse(s, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint, Globalization.CultureInfo.CurrentCulture, result) Then
    ' use result
End If

Troubleshooting tips:

  • Inspect what TRS actually returns (TypeName or Debug.Print) before you format it.
  • Use Decimal/Currency for money to avoid floating-point rounding.
  • Avoid relying on Val for user-facing or locale-sensitive parsing; prefer TryParse/Parse with NumberStyles and CultureInfo.
  • If you remove commas with Replace, be mindful of locales where comma is the decimal separator — prefer culture-aware parsing when available.

Summary: do math on true numeric types and format only for the UI. This prevents the Val/comma parsing trap and keeps totals correct.

Recommended Answers

All 2 Replies

I don’t know exactly why this is happening but looking at your code I detect a code smell. I mean that in the nicest way possible .

What I would do is simplify your code by separating the formatting away from the calculations. Those are two separate tasks, or concerns, and so should be handled separately. This separating out of tasks so they are independent of each other is one of the tenets of the SOLID principles of program design. The “S” in SOLID stands for Separation of Concerns.

When you combine the two in one function (formatting and calculations) the code is more complicated and finding a solution to a bug is therefore more complicated. When finding a bug like this the programmer could ask themselves, “Is the problem coming from the formatting or is the calculation algorithm off or maybe I simple entered in the wrong data?” In other words there are too many possible ways this algorithm is breaking down. When you try to do more that one thing at a time it is very difficult to test the code to trace a bug.

Here is an example of what you can try so you can narrow down where things are going wrong:
I don’t know what the Function TRS does but here I am assuming it returns a double.

Public Class FindTotal

Public Function CalculateTotal(dblsharbal as Double, dblworkbal as Double, dblsalabal as Double, dblprofit as Double) as Double
    Dim total as double = dblSharbla + dblworkbal + dblsalbal + dblprofit

    Return total
End Function

End Class


Dim dbl_Jsharbal = TRS("Sharbal")
Dim dbl_Jworkbal = TRS("workbal")
Dim dbl_Jsalabal = TRS("Salabal")
Dim dbl_Jprofit = TRS("Prflbal")



Dim myFindTotal as new FindTotal
 CRTOTAL = myFindTotal. CalculateTotal(dbl_Jsharbal, dbl_Jworkbal, dbl_Jsalabal, dbl_Jprofit)

LblCrtotal.Caption = Format$(CRTOTAL, "#,##0.00")




Jsharbal = Format$(TRS("Sharbal"), "#,##0.00")
Jworkbal = Format$(TRS("workbal"), "#,##0.00")
Jsalabal = Format$(TRS("Salabal"), "#,##0.00")
Jprofit = Format$(TRS("Prflbal"), "#,##0.00")

This way you have separated out the functions and can look at the output from the formatting and calculation algorithms separately. This should make trouble shooting easier.

HTH

commented: Good answer. +15

OK thank u..I got the idea from your tips..I just shifted the formatting code after the calculation.I formatted all the variables and labels at the end. It works fine.Thank you once again

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.