Convert.Toint32() a String with Commas

Convert.ToInt32() a string with Commas

You could use int.Parse and add the NumberStyles.AllowThousands flag:

int num = int.Parse(toParse, NumberStyles.AllowThousands);

Or int.TryParse letting you know if the operation succeeded:

int num;
if (int.TryParse(toParse, NumberStyles.AllowThousands,
CultureInfo.InvariantCulture, out num))
{
// parse successful, use 'num'
}

Convert a string with commas to double

There are a lot of possible solutions. Although, the best one I think is to set your CultureInfo to Invariant and then replace all commas with dots. For example:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string number = "3,2";
double d = double.Parse(number.Replace(",", "."));
Console.WriteLine(d);

In your code that would be made like this:

    double[] numbers1 = 
lineStr1.Replace(",", ".")
.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
double value;
bool success = double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value);

return new { value, success };
})
.Where(p => p.success)
.Select(p => p.value)
.ToArray();

double[] numbers2 =
lineStr2.Replace(",", ".")
.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
double value;
bool success = double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value);

return new { value, success };
})
.Where(p => p.success)
.Select(p => p.value)
.ToArray();

Now you have two arrays numbers1 and numbers2 with all possible numbers parsed from the two strings lineStr1 and lineStr2. You decide what to do with them as I cannot understand fully the purpose of your method.

(Thanks to all the guys in the comments for making me edit my answer.)

Converting a number from a string to an int with decimals separated by comma or dot

Maybe safest bet would be to try parse input with both cultures, something like this:

private static int ConvertStringValue(string value)
{
decimal valDouble;

var comma = (NumberFormatInfo)CultureInfo.InstalledUICulture.NumberFormat.Clone();
comma.NumberDecimalSeparator = ",";
comma.NumberGroupSeparator = ".";

var dot = (NumberFormatInfo)CultureInfo.InstalledUICulture.NumberFormat.Clone();
dot.NumberDecimalSeparator = ".";
dot.NumberGroupSeparator = ".";

if (decimal.TryParse(value, NumberStyles.Currency, comma, out valDouble))
{
return Convert.ToInt32(valDouble * 100);
}
else if (decimal.TryParse(value, NumberStyles.Currency, dot, out valDouble))
{
return Convert.ToInt32(valDouble * 100);
}
else
{
return Convert.ToInt32(value);
}
}

convert string number to integer

 string k = "12,000";
int i = Convert.ToInt32(k.Replace(",", ""));

will work

How to Convert string(1.0000) to int

You can convert it to Double first, and then convert to Int32

String s = "1.0000";
Double temp;

Boolean isOk = Double.TryParse(s, out temp);

Int32 value = isOk ? (Int32) temp : 0;

How to convert a c# comma separated values string into a list?

var intValues = line.Split(',').Select(x => Convert.ToInt32(x)).ToList();

Update

To ensure your code would be able to process strings like 1,2,3,,,4,5,6 you can use overload of String.Split method

var intValues = line.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToInt32(x))
.ToList();


Related Topics



Leave a reply



Submit