Reputation: 2578
I am trying to write a function which basically converts an array of strings to an array of strings where all the doubles in the array are rounded to the number of decimalplaces i set. There can also be strings in the array which are no double values at all.
string[,] values = new string[1, 3];
values[0, 0] = "hello";
values[0, 1] = "0.123";
values[0, 2] = "0,123";
int decimalPlaces = 2;
double tmp;
string format = "F" + decimalPlaces.ToString();
IFormatProvider provider = CultureInfo.InvariantCulture;
for (int i = 0; i < values.GetLength(0); i++)
{
for (int j = 0; j < values.GetLength(1); j++)
{
if (double.TryParse(values[i, j], out tmp))
{
values[i, j] = tmp.ToString(format, provider);
}
}
}
Console.ReadLine();
The result has to be: "hello" , "0.12", "0.12" but it is "hello", "123.00", "0.12" will treat the comma in the wrong way. Does anyone have a simple and efficient solution for this?
Upvotes: 110
Views: 254279
Reputation: 109
This code might help you to convert string
to double
even if there could be some comma instead of point.
//Get all culture separators
var culutres = CultureInfo.GetCultures(CultureTypes.AllCultures);
var _separators = culutres.Select(x => x.NumberFormat.NumberDecimalSeparator).ToArray();
var separators = new List<string>();
foreach(var separator in _separators)
if (!separators.Contains(separator))
separators.Add(separator);
string unparsedText = "900,93";
//Replace all possible separators with point
separators.ForEach(x => unparsedText = unparsedText.Replace(x, "."));
var value = double.Parse(unparsedText, NumberStyles.Number, CultureInfo.InvariantCulture);
Upvotes: -1
Reputation: 206
First of all, I would like to thank Sinipelto.
Here is just a small improvements of his answer
First I decide to declare cultural as static, and took in consideration case when you have value without comma or dot. Hope this saves few seconds for someone
public static CultureInfo pointCulture = new CultureInfo("en")
{
NumberFormat =
{
NumberDecimalSeparator = "."
}
};
public static CultureInfo commaCulture = new System.Globalization.CultureInfo("en")
{
NumberFormat =
{
NumberDecimalSeparator = ","
}
};
private double ConvertToDouble(string input)
{
input = input.Trim();
if (!input.Contains(",") && !input.Contains("."))
{
return double.Parse(input);
}
if (input.Contains(",") && input.Split(',').Length == 2)
{
return Convert.ToDouble(input, commaCulture);
}
if (input.Contains(".") && input.Split('.').Length == 2)
{
return Convert.ToDouble(input, pointCulture);
}
throw new Exception("Invalid input!");
}
Upvotes: -1
Reputation: 333
For me using the culture info was not an option, since the app was running on systems with different cultures but the input strings to be parsed where of unknown culture.
So I referred to normalizing the string representation and then converting it using CultureInfo.InvariantCulture:
private static double NormalizeAndParse(string strDouble)
{
string strDoubleNormalized;
if (strDouble.Contains(","))
{
var strReplaced = strDouble.Replace(",", ".");
var decimalSeparatorPos = strReplaced.LastIndexOf('.');
var strInteger = strReplaced.Substring(0, decimalSeparatorPos);
var strFractional = strReplaced.Substring(decimalSeparatorPos);
strInteger = strInteger.Replace(".", string.Empty);
strDoubleNormalized = strInteger + strFractional;
}
else
{
strDoubleNormalized = strDouble;
}
return Double.Parse(strDoubleNormalized, NumberStyles.Any, CultureInfo.InvariantCulture);
}
Upvotes: 0
Reputation: 1763
I use the following snippet if I want to parse a number with a decimal separator and nothing else:
public bool TryParseDecimal(string value, out decimal result) {
const string your_separator = ",";
var numberFormat = new NumberFormatInfo {
NumberDecimalSeparator = your_separator
};
return decimal.TryParse(value, NumberStyles.AllowDecimalPoint, numberFormat, out result);
}
I don't think that using a culture or string manipulation expresses the intention of convertig a number with a non '.' decimal point.
Upvotes: 0
Reputation: 237
Another option would be to use the information on the decimal separator from CultureInfo class. Knowing that we can replace ',' with '.' or vice versa when needed. A number grouping symbol is also available in this class if something needs to be done with a number like 1,000,000.23.
string decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string value = "";
if (decimalSeparator == ".")
{
value = rateLimitTextBox.Text.Replace(",", ".");
}
else if (decimalSeparator == ",")
{
value = rateLimitTextBox.Text.Replace(".", ",");
}
bool LimitAcceptable = decimal.TryParse(value, NumberStyles.Any, CultureInfo.CurrentCulture, out decimal limit);
Upvotes: -1
Reputation: 1070
You DO NOT NEED to replace the comma and dot..
I have had the very same problem. The reason is simple, the conversion culture plays a big role in which the comma or a dot is interpreted. I use a German culture where the comma distinguish the fractions, where as elsewhere the dot does the job.
Here I made a complete example to make the difference clear.
string[] doubleStrings = {"hello", "0.123", "0,123"};
double localCultreResult;
foreach (var doubleString in doubleStrings)
{
double.TryParse(doubleString, NumberStyles.Any, CultureInfo.CurrentCulture, out localCultreResult);
Console.WriteLine(string.Format("Local culture results for the parsing of {0} is {1}", doubleString, localCultreResult));
}
double invariantCultureResult;
foreach (var doubleString in doubleStrings)
{
double.TryParse(doubleString, NumberStyles.Any, CultureInfo.InvariantCulture, out invariantCultureResult);
Console.WriteLine(string.Format("Invariant culture results for the parsing of {0} is {1}", doubleString, invariantCultureResult));
}
The results is the following:
Play around with the culture and you will get the result you need.
Upvotes: 70
Reputation: 326
Make two static cultures, one for comma and one for point.
var commaCulture = new CultureInfo("en")
{
NumberFormat =
{
NumberDecimalSeparator = ","
}
};
var pointCulture = new CultureInfo("en")
{
NumberFormat =
{
NumberDecimalSeparator = "."
}
};
Then use each one respectively, depending on the input (using a function):
public double ConvertToDouble(string input)
{
input = input.Trim();
if (input == "0") {
return 0;
}
if (input.Contains(",") && input.Split(',').Length == 2)
{
return Convert.ToDouble(input, commaCulture);
}
if (input.Contains(".") && input.Split('.').Length == 2)
{
return Convert.ToDouble(input, pointCulture);
}
throw new Exception("Invalid input!");
}
Then loop through your arrays
var strings = new List<string> {"0,12", "0.122", "1,23", "00,0", "0.00", "12.5000", "0.002", "0,001"};
var doubles = new List<double>();
foreach (var value in strings) {
doubles.Add(ConvertToDouble(value));
}
This should work even though the host environment and culture changes.
Upvotes: 8
Reputation: 11426
Extension to parse decimal number from string.
Ability to set decimal symbol manually.
public static class StringExtension
{
public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = ',')
{
string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([\s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;
if (tmp.Length > 0 && strToParse.Contains(tmp))
{
var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;
tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
return double.Parse(tmp);
}
return 0;
}
}
How to use:
"It's 4.45 O'clock now".DoubleParseAdvanced(); // will return 4.45
"It's 4,45 O'clock now".DoubleParseAdvanced(); // will return 4.45
"It's 4:45 O'clock now".DoubleParseAdvanced(':'); // will return 4.45
Upvotes: 1
Reputation: 9158
You want to treat dot (.
) like comma (,
). So, replace
if (double.TryParse(values[i, j], out tmp))
with
if (double.TryParse(values[i, j].Replace('.', ','), out tmp))
Upvotes: 25
Reputation: 50326
The problem is that you (or the system) cannot distinguish a decimal separator from a thousands separator when they can be both a comma or dot. For example:
In my culture,
1.123
is a normal notation for a number above 1000; whereas
1,123
is a number near 1.
Using the invariant culture defaults to using the dot as a decimal separator. In general you should ensure that all numbers are written using the same constant culture on all systems (e.g. the invariant culture).
If you are sure that your numbers never contain anything other than a comma or dot for a decimal separator (i.e. no thousands separators), I'd String.Replace()
the comma with a dot and do the rest as you did.
Otherwise, you'll have a hard time programming something that can distinguish 1.123
from 1,123
without knowing the culture.
Upvotes: 19
Reputation: 18439
To treat both , and . as decimal point you must not only replace one with the other, but also make sure the Culture used parsing interprets it as a decimal point.
text = text.Replace(',', '.');
return double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out value);
Upvotes: 205
Reputation: 24526
Use this overload of double.TryParse to specify allowed formats:
Double.TryParse Method (String, NumberStyles, IFormatProvider, Double%)
By default, double.TryParse will parse based on current culture specific formats.
Upvotes: 2
Reputation: 807
You can check if the string contains a decimal point using
string s="";
if (s.Contains(','))
{
//treat as double how you wish
}
and then treat that as a decimal, otherwise just pass the non-double value along.
Upvotes: -1