• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

VB.net convert numeric text into number

Hi,

I have text in a string 1,234.0 when I try to convert this to a number using
Dim z as string = "1,234.0"
Dim x as integer = Val(z)

it converts the text to number 1

what can I do ?
0
MichMat
Asked:
MichMat
  • 6
  • 6
  • 2
1 Solution
 
jpaulinoCommented:
That's because the "," is the decimal separator and that way it only gets the one. Integer values doesn't allaw to use decimal values.
0
 
MichMatAuthor Commented:
Ok , I thought that Val converts to a double
0
 
jpaulinoCommented:
If you want to convert to double you should use Double.Parse() method and replace the "," with nothing.
Like this:
 

        Dim z As String = "1,234.0"
        Dim x As Double = Double.Parse(z.Replace(","c, ""))

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
garethh86Commented:
See below, you can't use integer for this, integers are full numbers only (no decimal place), you can use double for what you want.
Dim Z as string = "1,234.56"
 
' Now remove the comma from the string
Dim Z2 as string = z.replace(",", "")
 
'store as double
Dim x as double = Z2

Open in new window

0
 
MichMatAuthor Commented:
Hi Thanks for that

That is the solution to the problem.
0
 
jpaulinoCommented:
I don't belive !!!
I have give you the solution (the better) and you give point to the garethh86
That allot unfair!!
0
 
MichMatAuthor Commented:
Ahhhh Crap, Jpaulino the accepted solution was supposed to be yours, I didnt realise that someone else posted what is the same solution. Bugger
0
 
jpaulinoCommented:
Ok, someone will fix it!
0
 
MichMatAuthor Commented:
Im quite happy to re-post again under new question
0
 
MichMatAuthor Commented:
Your suggestion is better I'll request attention to this thread and a correction to the points.
0
 
jpaulinoCommented:
Thanks !
0
 
garethh86Commented:
Sorry, I made my post at the same time so didn't see your solution jpaulino, otherwise I wouldn't have posted. Nothing untoward, I wasn't trying to steal points. I'm more than happy to have the points moved over if possible, I must admit I was a little surprised when my solution was accepted.

Glad you got it working though!

Thanks

Gareth
0
 
jpaulinoCommented:
No problem Gareth and have a nice Easter! :)
0
 
MichMatAuthor Commented:
Thank you for the solution and sorry for the initial mix up
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now