Decimals – I try to use "." gives error in one place I try "," gives in another place

Decimals – I try to use "." gives error in one place I try "," gives in another place

Problem Description:

I insert for example 12.45
gives me error here:

//Input string was not in a correct format

deposit = decimal.Parse(Console.ReadLine());

If I insert 12,45
gives me error here:

//Incorrect syntax near ‘,’

var reader = cmd.ExecuteReader();

Here is already after the part where it gives error on top, and the value already works up there, but down here not

Here shows that up there works, but I insated with "," but appears with "."

Decimals - I try to use "." gives error in one place I try "," gives in another place

Solution – 1

If you want to be able to correctly parse a number using a comma "12,45" instead of a period "12.45" there are a number of overloads that will allow you to specify the decimal separator in different ways:

For example, using German culture info (I don’t know what culture you’re using)

var deposit = decimal.Parse("12,45", new CultureInfo("de-DE"));
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject