H
H
Hanssssshion2021-11-13 17:28:40
C++ / C#
Hanssssshion, 2021-11-13 17:28:40

How to solve error when converting string to real number?

You need to move the number behind a comma, did the conversion to double
and gives an error: 'Input string was not in a correct format.'
Although there is an English version of visual studio, it should count a dot, not a comma.
Maybe overlooked something

string stri = "0." + artQuantity;
double probability = Convert.ToDouble(stri);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-13
@Hanssssshion

1. It does not depend on the studio, but on the system locale.
2. When parsing, it is better to always explicitly specify the locale used.
In your case, this would work:

var str = "0." + artQuantity;
var probability = double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);

Also check that there are no extra characters in artQunatity, such as spaces, periods, commas.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question