Visual studio wont convert my string to decimal
Error: Input string was not in a correct format.
Code:
string test = "123.95";
decimal test1 = decimal.parse(test); // string being an int "123" doesnt cause this
also Convert.toDecimal(test); does just the same.
EDIT: Thanks for the answers, I was looking online for how decimals worked and everyone were using '.' and not ','. Sorry about how incredibly stupid I and this post is. And thanks again for the answeres :)
It's likely your current culture doesn't use .
as a decimal separator. Try specifying the invariant culture when you parse the string:
using System.Globalization;
...
string test = "123.95";
decimal test1 = decimal.Parse(test, CultureInfo.InvariantCulture);
Alternatively, you could specify a culture that uses the specific format you want:
string test = "123.95";
var culture = new CultureInfo("en-US");
decimal test1 = decimal.Parse(test, culture);
Use this code -
var test1 = "123.95";
decimal result;
decimal.TryParse(test1, out result);
It worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With