Click here to Skip to main content
15,869,940 members
Articles / Web Development / ASP.NET
Article

Parsing floating point strings with specified decimal separator

Rate me:
Please Sign up or sign in to vote.
4.53/5 (21 votes)
9 Apr 20031 min read 152.4K   21   19
Parsing strings in .NET is very easy, but if you want to specify different decimal separators, there might be some confusion.

Introduction

Everyone knows that "everything is easy in .NET" and in most cases it really is. This also applies to string parsing issues. When you want to convert string "123.4" into value 123.4, you usually write code looking like:

C#
double someNumber;
string myNumber = "123.4";

try
{
    someNumber = double.Parse(myNumber);
}
catch(Exception)
{
    //something's wrong
    //that is not a number
}

In most cases it works with no trouble. Until your string is not a number or... decimal separator is different than one defined in your system.

Let's assume that your system decimal separator is a comma (",") and you want to parse string with dot (".") separator - there's no way. An exception is thrown (and you are happy that try-catch works :) ).

There is of course an easy way to parse strings with decimal separator you want to use. The Parse method allows to define additional parameter - IFormatProvider. If you put NumberFormatInfo object as a second parameter of Parse method, you can specify what decimal separator should be used.

According to MSDN you can obtain current thread NumberFormatInfo by CurrentInfo static property. There is some confusion because CurrentInfo is a read-only property and you would like to modify the decimal separator.

There's a little "walk around" - use current CultureInfo, "clone" its NumberFormatInfo and modify the decimal separator:

C#
class ParserClass
{
    ...
    static System.Globalization.NumberFormatInfo ni = null;
    public ParserClass()
    {
       System.Globalization.CultureInfo ci = 
          System.Globalization.CultureInfo.InstalledUICulture;
       ni = (System.Globalization.NumberFormatInfo)
          ci.NumberFormat.Clone();
       ni.NumberDecimalSeparator = ".";
    }
    public someMethod()
    {
        double someNumber;
        string myNumber = "123.4";
        try
        {
            someNumber = double.Parse(myNumber,ni);
        }
        catch(Exception)
        {
        }
    }
}

Using static variable ni allows you to use it whenever you parse string with "." decimal separator (assuming that instance of ParserClass is created).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSupporting both . and , Pin
Mattias Olgerfelt1-Sep-04 2:05
Mattias Olgerfelt1-Sep-04 2:05 
GeneralRe: Supporting both . and , Pin
17-Jun-05 0:38
suss17-Jun-05 0:38 
GeneralRe: Supporting both . and , Pin
Michal Rorat17-Jun-05 1:19
Michal Rorat17-Jun-05 1:19 
GeneralRe: Supporting both . and , Pin
Mattias Olgerfelt17-Jun-05 1:45
Mattias Olgerfelt17-Jun-05 1:45 
GeneralHint Pin
Grommel21-Apr-04 2:19
Grommel21-Apr-04 2:19 
GeneralDracula Pin
LeonJ16-Apr-03 0:29
LeonJ16-Apr-03 0:29 
GeneralComment Pin
John O'Byrne11-Apr-03 1:35
John O'Byrne11-Apr-03 1:35 
GeneralRe: Comment Pin
Tim Smith11-Apr-03 2:19
Tim Smith11-Apr-03 2:19 
GeneralRe: Comment Pin
John O'Byrne11-Apr-03 4:16
John O'Byrne11-Apr-03 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.