Solved

Convert string to Double, accepting both comma and dot

Posted on 2009-07-04
12
2,017 Views
Last Modified: 2013-11-26
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?
0
Comment
Question by:Emanuele_Ciriachi
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24777880
you should replace either one by the other (in the string), and use a CultureInfo object to specify which one you are finally using.
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 24777885
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.
0
 

Expert Comment

by:james4651
ID: 24777892
You could just replace the commas with dot and call Convert.ToDouble(string)
0
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 24777919
Hi,
If your string variable is StrVal then try this:

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

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777920
>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
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 24777931
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777989
>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.
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 24778001
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?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24778019
>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.


0
 
LVL 39

Expert Comment

by:abel
ID: 24778021
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

0
 
LVL 39

Expert Comment

by:abel
ID: 24778023
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.
0
 
LVL 1

Author Closing Comment

by:Emanuele_Ciriachi
ID: 31599818
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now