Wrong value using val(textbox.text)

  • Thread starter Thread starter Drygast
  • Start date Start date
D

Drygast

I get the value that doesn't add up correct.

I have 2 textboxes where I enter numbers, I then calculate them and present
them as a reslut in a 3rd
textbox, however when the result is calculated it won't calculate the
correct value.
For instance when the value is 75,25 the code interprets it as 75.

The code:
lblSumma2.Text = Val(lblSumma2.Text) + ((txtAntal.Text) * myMinutNetto) +
myFastNetto

The problem is the val(lblsumma2.text), it skips the decimals, how can I
change this to use the actual number?
(lblSumma2.text will show the correct value, the problem is just when using
the val()-option)

Regards
/Drygast
 
* "Drygast said:
I have 2 textboxes where I enter numbers, I then calculate them and present
them as a reslut in a 3rd
textbox, however when the result is calculated it won't calculate the
correct value.
For instance when the value is 75,25 the code interprets it as 75.

The code:
lblSumma2.Text = Val(lblSumma2.Text) + ((txtAntal.Text) * myMinutNetto) +
myFastNetto

The problem is the val(lblsumma2.text), it skips the decimals, how can I
change this to use the actual number?
(lblSumma2.text will show the correct value, the problem is just when using
the val()-option)

<msdn>
Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl or CInt instead to convert a string to a number.
</msdn>

You can use 'Double.Parse' to parse the value of the string.
 
Lightning fast repsonse!

Thanks I'll try yout suggestion.

I also tried a "short-cut" using
val(lblSumma2.text.Replace(",", "."))

and that worked for me.



Regards

/Drygast

Herfried K. Wagner said:
<msdn>
Note The Val function recognizes only the period (.) as a valid decimal
separator. When different decimal separators are used, as in international
applications, use CDbl or CInt instead to convert a string to a number.
 
Drygast said:
I get the value that doesn't add up correct.

I have 2 textboxes where I enter numbers, I then calculate them and
present them as a reslut in a 3rd
textbox, however when the result is calculated it won't calculate
the correct value.
For instance when the value is 75,25 the code interprets it as 75.

The code:
lblSumma2.Text = Val(lblSumma2.Text) + ((txtAntal.Text) *
myMinutNetto) + myFastNetto

The problem is the val(lblsumma2.text), it skips the decimals, how
can I change this to use the actual number?

Don't use Val. It expects the "." as decimal separator. Use Decimal.Parse,
Double.Parse, CSng, CDec, CDbl, ...
 
* "Drygast said:
Lightning fast repsonse!

Thanks I'll try yout suggestion.

I also tried a "short-cut" using
val(lblSumma2.text.Replace(",", "."))

and that worked for me.

Notice that this will not work on every systems as expected because
different languages/cultures use different characters as decimal comma.
 
Back
Top