Is this a bug?

  • Thread starter Thread starter Harry
  • Start date Start date
H

Harry

Hi all

I tried :

Dim test as Decimal = Cdec(Val("-20.0000")). This return 0.

Also using Decimal.TryParse with the above string returns false.
Surely this is not correct?

Cheers
 
Harry said:
Hi all

I tried :

Dim test as Decimal = Cdec(Val("-20.0000")). This return 0.

Also using Decimal.TryParse with the above string returns false.
Surely this is not correct?

Cheers


Is that really the code that produces the zero? Perhaps your code has a
charactor that does not print before the -20.0000. In such a case, you
would get zero on converting, and false for tryparse.
 
Harry said:
Hi all

I tried :

Dim test as Decimal = Cdec(Val("-20.0000")). This return 0.

Also using Decimal.TryParse with the above string returns false.
Surely this is not correct?

Don't use Val() for something that the user enter, or in other than US
format. Use CDec without Val, or TryParse. Here is an example:

Dim test As Decimal = CDec(Val("-20.0000"))
Console.WriteLine("Test = " & test)
test = CDec(Val("-20.7000"))
Console.WriteLine("Test = " & test)
test = CDec(Val("-20,7000"))
Console.WriteLine("Test = " & test)
test = CDec("-20.7000")
Console.WriteLine("Test = " & test)
test = CDec("-20,7000")
Console.WriteLine("Test = " & test)

Output in VB 2008 when Control Panel is set to US/English:

Test = -20
Test = -20.7
Test = -20
Test = -20.7000
Test = -207000
 
Back
Top