Home All Groups Group Topic Archive Search About
Author
18 Jun 2009 3:35 AM
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

Author
18 Jun 2009 5:00 AM
Martin H.
Hello Harry,

I just tested it and it returned -20.

Best regards,

Martin

On 18.06.2009 05:35, Harry wrote:
Show quoteHide quote
> Dim test as Decimal = Cdec(Val("-20.0000"))
Author
18 Jun 2009 11:04 AM
Family Tree Mike
Show quote Hide quote
"Harry" <harryNoSpam@ffapaysmart.com.au> wrote in message
news:ugGhWX87JHA.5828@TK2MSFTNGP04.phx.gbl...
> 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.

--
Mike
Author
18 Jun 2009 11:12 AM
Patrice
What I see here in France :

Cdec(Val("-20.0000")) returns 20. This is what I expect as the Val function
parse . as the decimal point. IMO it should always work.

Decimal.TryParse returns false. This is also what I expect as . is not valid
in my country... The result depends on your country...

--
Patrice

"Harry" <harryNoSpam@ffapaysmart.com.au> a écrit dans le message de groupe
de discussion : ugGhWX87JHA.5***@TK2MSFTNGP04.phx.gbl...
Show quoteHide quote
> 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
>
>
Author
18 Jun 2009 1:16 PM
Nobody
"Harry" <harryNoSpam@ffapaysmart.com.au> wrote in message
news:ugGhWX87JHA.5828@TK2MSFTNGP04.phx.gbl...
> 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