error in calulation

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi....
i have 3 textbox in my form.in a very simple form ,i want textbox3 calculate the multiply of textbox1 value in textbox2 value.

VB.NET:
Textbox1.text=1234567890.1
Textbox2.text=1234567

it's result in calculator is : 1524156776377086.7

but if in Textbox1.TextChange And Textbox2.TextChange event put this code :

VB.NET:
 Textbox3.text=val(textbox1.text) * val(textbox2.text)
the result is : 1.52415677637709E+15

or by this code :

VB.NET:
textbox3.text=FormatNumber([FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][SIZE=2][FONT=Consolas][COLOR=#0000ff]CType[/COLOR](Val(TextBox1.Text), [COLOR=#0000ff][COLOR=#0000ff][COLOR=#0000ff]Double[/COLOR][/COLOR][/COLOR]) * [COLOR=#0000ff][COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR][/COLOR](Val(TextBox2.Text), [COLOR=#0000ff][COLOR=#0000ff][COLOR=#0000ff]Double[/COLOR][/COLOR][/COLOR]), 5, , , )[/FONT][/SIZE]
[/SIZE][/FONT][/SIZE][/FONT]Result is : 1524156776377090.00000

why this happen and what should i do so my result equal by calculator's result.
 
I would suggest that you pretty much never use the Val function. If you do then cast as type Double is pointless because that's what Val returns anyway. In your case, you will want to use the Decimal data type rather than Double. You should use Decimal.TryParse to both validate and convert the text in the TextBoxes to Decimal values.
 
Back
Top