Converting String to Double in C#

Converting string to double in C#

There are 3 problems.

1) Incorrect decimal separator

Different cultures use different decimal separators (namely , and .).

If you replace . with , it should work as expected:

Console.WriteLine(Convert.ToDouble("52,8725945"));

You can parse your doubles using overloaded method which takes culture as a second parameter. In this case you can use InvariantCulture (What is the invariant culture) e.g. using double.Parse:

double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);

You should also take a look at double.TryParse, you can use it with many options and it is especially useful to check wheter or not your string is a valid double.

2) You have an incorrect double

One of your values is incorrect, because it contains two dots:

15.5859949000000662452.23862099999999

3) Your array has an empty value at the end, which is an incorrect double

You can use overloaded Split which removes empty values:

string[] someArray = a.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);

Convert string to double or float c#

I suspect your default culture info uses comma as a decimal separator instead of dot. If you know your input will have a dot, it's best to specify the invariant culture explicitly:

double d1 = double.Parse(s1, CultureInfo.InvariantCulture);
double d2 = double.Parse(s2, CultureInfo.InvariantCulture);

However, if the decimal values matter, you should consider using decimal instead of either float or double:

decimal d1 = decimal.Parse(s1, CultureInfo.InvariantCulture);
decimal d2 = decimal.Parse(s2, CultureInfo.InvariantCulture);

How to convert string to double in C#

The exception is telling you what the issue is. Your constructor is expecting doubles, and you're passing it strings. In order to fix it, you've gotta parse your string inputs into doubles.

The way your code is written, you'll have to change the way you're using the .Select statement in order to parse it in a decent error handling manner.

I'd suggest swapping the .Select for a foreach, then parsing each property, then instantiating your class.

foreach (var item in parsed)
{
double userId = 0;
double itemId = 0;
double rating = 0;
double.TryParse(item.UserId, out userId);
double.TryParse(item.ItemId, out itemId);
double.TryParse(item.rating, out rating);

var rating = new AddRating(userId, itemId, rating);
//**** do whatever you want with the new object
}

How could I convert a string variable to a double in C#

You aren't assigning the result of your Convert call to a variable, you're just throwing it away. Convert doesn't alter the type of the existing variable (because, apart from other considerations, you simply can't do that in a strongly-typed language), instead it produces a new variable for you to use in your maths.

The error is, I presume without seeing the relevant code, because you tried to use userAge in your calculations which, as I've just explained, is still a string.

Also, to go back a step, you've never actually assigned the result of the ReadLine operation to the userAge variable in the first place.

This:

Console.WriteLine("What is your age?");
string userAge = Console.ReadLine();
double age = Convert.ToDouble(userAge);

would make more sense. And then use age in your calculations afterwards.

Convert string to double but missing a dot

You can use this:

string finalString = "7.4361";
decimal finalNumber = decimal.Parse(finalString, System.Globalization.CultureInfo.InvariantCulture);

// 7.4361

Convert string to Double C#

Try specifying a culture when parsing:

// CultureInfo.InvariantCulture would use "." as decimal separator
// which might not be the case of the current culture
// you are using in your application. So this will parse
// values using "." as separator.
double d = double.Parse(txtbox.Text, CultureInfo.InvariantCulture);

And to handle the error case for gracefully instead of throwing exceptions around you could use the TryParse method:

double d;
if (double.TryParse(txtbox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out d))
{
// TODO: use the parsed value
}
else
{
// TODO: tell the user to enter a correct number
}

Convert a string to Double in C#

.Net has built in functionality for this, its called Double.TryParse. Double.Parse also exists, but its recommended to use the Try variant, as it won't throw exceptions if the number is not parseable into a double. You can use the method like this

string stringToParse = "1.7346"
if (Double.TryParse(stringToParse, out double parsedDouble))
{
//do something with the double here
}
else
{
//failed to parse, error logic here
}

Calculations in c# and converting string to double

Try to use Decimal.Parse, documentation here.

        Console.WriteLine("Please enter the first rate");
decimal NGNRate = Decimal.Parse(Console.ReadLine());

Console.WriteLine("Please enter the Master rate");
decimal TM_USD_ZAR = Convert.ToDecimal(Console.ReadLine());

Console.WriteLine(NGNRate / TM_USD_ZAR);


Related Topics



Leave a reply



Submit