Click here to Skip to main content
11,930,830 members (49,389 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Parsing floating point strings with specified decimal separator

, 9 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Parsing strings in .NET is very easy, but if you want to specify different decimal separators, there might be some confusion.


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:

double someNumber;
string myNumber = "123.4";

    someNumber = double.Parse(myNumber);
    //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 Smile | :) ).

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:

class ParserClass
    static System.Globalization.NumberFormatInfo ni = null;
    public ParserClass()
       System.Globalization.CultureInfo ci = 
       ni = (System.Globalization.NumberFormatInfo)
       ni.NumberDecimalSeparator = ".";
    public someMethod()
        double someNumber;
        string myNumber = "123.4";
            someNumber = double.Parse(myNumber,ni);

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


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


About the Author

Marcin Cuprjak
Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
JElnur3-Nov-11 21:00
memberJElnur3-Nov-11 21:00 
GeneralMy vote of 5 Pin
minhtruc20-Jun-11 18:36
memberminhtruc20-Jun-11 18:36 
GeneralOr you do not care... Pin
rroestenburg5-Oct-07 3:00
memberrroestenburg5-Oct-07 3:00 
GeneralRe: Or you do not care... Pin
Toumar5-Aug-09 12:28
memberToumar5-Aug-09 12:28 
Generalchange regional (language) Pin
hamid_m17-Feb-07 10:55
memberhamid_m17-Feb-07 10:55 
i want change current windows regional in my system
in fact i want change digits shape into my country digits shape (Farsi)
windows default regional is English(United states) and i want change it in my application to Farsi
in english regional digits are 1234567890
but in farsi regional digits are ۱۲۳۴۵۶۷۸۹۰
i just want do it Frown | :(
please help me

QuestionChanging the locale? Pin
QmQ28-May-06 13:34
memberQmQ28-May-06 13:34 
AnswerRe: Changing the locale? Pin
Marcin Cuprjak (aka Vlad Tepes)29-May-06 11:51
memberMarcin Cuprjak (aka Vlad Tepes)29-May-06 11:51 
GeneralRe: Changing the locale? Pin
QmQ29-May-06 12:04
memberQmQ29-May-06 12:04 
GeneralStatic constructor Pin
Steve Hansen10-May-06 3:49
memberSteve Hansen10-May-06 3:49 
GeneralERROR Pin
sgomcres9-Feb-06 6:26
membersgomcres9-Feb-06 6:26 
GeneralSupporting both . and , Pin
Mattias Olgerfelt1-Sep-04 3:05
memberMattias Olgerfelt1-Sep-04 3:05 
GeneralRe: Supporting both . and , Pin
Michal Rorat17-Jun-05 1:38
memberMichal Rorat17-Jun-05 1:38 
GeneralRe: Supporting both . and , Pin
Michal Rorat17-Jun-05 2:19
memberMichal Rorat17-Jun-05 2:19 
GeneralRe: Supporting both . and , Pin
Mattias Olgerfelt17-Jun-05 2:45
memberMattias Olgerfelt17-Jun-05 2:45 
GeneralHint Pin
Anonymous21-Apr-04 3:19
sussAnonymous21-Apr-04 3:19 
GeneralDracula Pin
LeonJ16-Apr-03 1:29
memberLeonJ16-Apr-03 1:29 
GeneralComment Pin
John O'Byrne11-Apr-03 2:35
memberJohn O'Byrne11-Apr-03 2:35 
GeneralRe: Comment Pin
Tim Smith11-Apr-03 3:19
memberTim Smith11-Apr-03 3:19 
GeneralRe: Comment Pin
John O'Byrne11-Apr-03 5:16
memberJohn O'Byrne11-Apr-03 5: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 10 Apr 2003
Article Copyright 2003 by Marcin Cuprjak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid