The problem here has nothing to do with the characters ',' and '.'.
Since you are using the method 'Parse' of the Decimal with the second parameter 'NumberStyles.Currency', the parser can identify ',' and '.'.
Trouble shooting:
I guess you are not in the region of US so that you should use CultureInfo for 'en-US' instead of
"CultureInfo.InvariantCulture".
Solution:
static void Main(string[] args)
{
string s = "$2,065,890.15";
var culture = CultureInfo.GetCultureInfo("en-US");
// the parser will automatically remove the $ and ,/. for the string
string s1 = decimal.Parse(s, NumberStyles.Currency , culture).ToString();
Console.WriteLine(s1);
Console.ReadKey();
}
Result:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Participant
1443 Points
2030 Posts
Error: Input string was not in correct format
Mar 31, 2020 06:31 AM|venkatzeus|LINK
Hi.
I have a datarow which contains value as $2,065,890.15
I am trying to pass the data to SQL table having column type as float. I did the below code:
I am getting the error as - Input string was not in correct format
Update1: the issue seems to be with characters , .
I was able to remove the , ( comma ) character with the replace. How to work with the decimal ?
How to fix this ?
Thanks
Contributor
2890 Points
848 Posts
Re: Error: Input string was not in correct format
Mar 31, 2020 08:16 AM|Sean Fang|LINK
Hi venkatzeus,
The problem here has nothing to do with the characters ',' and '.'.
Since you are using the method 'Parse' of the Decimal with the second parameter 'NumberStyles.Currency', the parser can identify ',' and '.'.
Trouble shooting:
I guess you are not in the region of US so that you should use CultureInfo for 'en-US' instead of "CultureInfo.InvariantCulture".
Solution:
Result:
Hope this can help you.
Best regards,
Sean