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

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.