Link to home
Start Free TrialLog in
Avatar of Emanuele_Ciriachi
Emanuele_Ciriachi

asked on

Convert string to Double, accepting both comma and dot

I want to be able, in VB .Net, to convert a string into a double, considering either comma or dot as decimal separator.

How can I do?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Emanuele_Ciriachi
Emanuele_Ciriachi

ASKER

This doesn't answer my question.
I would like to be able to parse a string into a double regadlesss of the CultureInfo I am currently using.
This could potentially take the form of checking the current decimal separator, and replacing the other into it whenever I'm trying to parse a double into a string.
You could just replace the commas with dot and call Convert.ToDouble(string)
Hi,
If your string variable is StrVal then try this:

Dim d as Double = Format(CType(StrVal,Double),"0,00")

Open in new window

>I would like to be able to parse a string into a double regadlesss of the CultureInfo I am currently using.
I guess I did not make clear that you could specify a dedicated cultureinfo just for the Double.Parse() call:
http://www.java2s.com/Tutorial/CSharp/0440__I18N-Internationalization/ParsingnumbersbasedonCultureinfo.htm

you might also try the invariant culture:
http://www.java2s.com/Tutorial/CSharp/0440__I18N-Internationalization/Parsingnumberswithcultureinvariantparsing.htm
Africans,

I don't know if my sting is a StrVal. Could you please shed some light on the issue?


angelIII,
the problem is that I don't know in which culture environment the program will run. In this context, being able to apply freely one convesion system or the other doesn't help.
>the problem is that I don't know in which culture environment the program will run.
I repeat: you can FORCE the culture info to be used for the parsing, completely ignoring the user environment.
All right, but then again the string could still contain either commas or dots, and forcing an environment that accepts either one or the other will not cover all possible cases...

Oh wait, I think I've got it: I can force the usage of commas, and then preemptively change all dots in commas, right?

If so, which command can I use to force the CultureInfo of each?
>Oh wait, I think I've got it: I can force the usage of commas, and then preemptively change all dots in commas, right?

was my first comment :)

>If so, which command can I use to force the CultureInfo of each?
see the comment with the 2 links.


Just for the fun of it (i know, there are already many comments), but why not simply doing something like this?

 


' with a dot, no (real) effect '
Dim dbl As Double = Double.Parse("12.34".Replace(",", "."), CultureInfo.InvariantCulture)
 
' same method with a comma, there is effect '
Dim dbl As Double = Double.Parse("12,34".Replace(",", "."), CultureInfo.InvariantCulture)
 
' in both cases, result is 12.34 in the variable named dbl '
 
' More generic, place this in your class '
Function ParseDouble(value As String) As Double
   Return Double.Parse(value.Replace(",", "."), CultureInfo.InvariantCulture)
End Function
 
' call it like so '
Dim myDbl As Double = ParseDouble("1234,567")

Open in new window

But: that's just a simplified way of parsing a double and when it contains a thousands-separator, you'd be in trouble.

Working with the Cultures, the way AngelIII explained, is the long-term best way of dealing with this.
Thanks, this worked.
For everyone that might stumble on this question, Angel is suggesting to force a specific CultureInfo and then perform the replacement of comma in dot (or viceversa) according to it.