The best method depends on the context. Do you parse XML? You are writing XML. In any case, all this is connected with culture.
If you write this, I assume your culture is set to use commas as decimal separators, and you are not aware of this fact. First, change your culture in your Windows settings to something that best suits your culture and how you do it. Secondly, if you were writing numbers for the human display, I would leave it as culturally sensual, so that it matches the person who reads it. If it needs to be analyzed by another machine, you can use Invariant Culture like this:
12.1223.ToString(CultureInfo.InvariantCulture);
If you read (I suppose this is what you are doing), you can reuse cultural information. If it was from a human source (for example, they typed it in a box), then use your default culture information again (by default in float.Parse). If it is from a computer, then use InvariantCulture again:
float f = float.Parse("12.1223", CultureInfo.InvariantCulture);
Of course, this suggests that the text was written using an invariant cooler. But since you are asking the question in the wrong way (if you do not control it, then in this case, using InvariantCulture for writing was suggested above). Then you can use a specific culture that understands commas to analyze it:
NumberFormatInfo commaNumberFormatInfo = new NumberFormatInfo(); commaNumberFormatInfo.NumberDecimalSeperator = ","; float f = float.Parse("12,1223", commaNumberFormatInfo);
source share