Last post Mar 31, 2020 08:16 AM by Sean Fang
Mar 31, 2020 06:31 AM|venkatzeus|LINK
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:
row = Decimal.Parse(row.ToString(), NumberStyles.Currency, CultureInfo.InvariantCulture);
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 ?
Mar 31, 2020 08:16 AM|Sean Fang|LINK
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 '.'.
I guess you are not in the region of US so that you should use CultureInfo for 'en-US' instead of
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();
Hope this can help you.