C
C#2y ago
jr.9

❔ Can you convert from an excel sheet double to decimal for currency math?

I'm importing an excel spreadsheet (xlsx) into my C# project using ExcelDataReader.DataSet and it just imports everything from the spreadsheet as it's saved. So numbers are imported as doubles. The sheet I'm working with has dollar values ranging from $0.25 to $10 or so - no real big numbers, maybe $99 at the absolute most once in a blue moon. My question is - is it safe to convert the double to a decimal to do currency math with? It obviously can't add or remove extra numbers to the end ($1.250000006 for instance) since that may screw up the calculations and they can't be off by even one cent.
24 Replies
Angius
Angius2y ago
Any particular reason you'd need to do that? It's safer to work with currency using decimals all in all
jr.9
jr.9OP2y ago
I don't have a choice, excel stores numbers as doubles
Angius
Angius2y ago
ah, sorry, I misunderstood your issue
jr.9
jr.9OP2y ago
you're fine, I appreciate you taking the time to look at it
cathei
cathei2y ago
Can't you read it as string?
jr.9
jr.9OP2y ago
not using ExcelDataReader, it reads values / data as the datatype it's saved as no matter what I use, I think the issue may be the same - I'm mostly just curious how inaccurate doubles are when working with small values like this and if it's feasible to convert to decimal
Angius
Angius2y ago
GitHub
Returning wrong value for float by ExcelDataReader · Issue #495 · E...
We have one Excel , Its have "73.5793103448276" value in first second column of first row . But ExcelDataReader column out is 73.57931034482759 Following is example of row 0001720...
Angius
Angius2y ago
Doubles should be decently accurate, as long as you're not working with thousands of values with millionth parts of a cent each
jr.9
jr.9OP2y ago
Ty Nah, two decimal points at most and mostly just tens of dollars
Angius
Angius2y ago
Might as well just work with cents then, ig Multiply each value by 100 and floor it to an integer Zero rounding error possibility
jr.9
jr.9OP2y ago
I thought decimal was safe to work with?
Angius
Angius2y ago
It is, yeah That's another possibility, sure
jr.9
jr.9OP2y ago
follow up question: Should I typecast it or use Convert.ToDecimal()?
cathei
cathei2y ago
No luck with turning off UseColumnDataType ?
jr.9
jr.9OP2y ago
That doesn't seem to actually DO anything, I don't know why... I think it may be a bug? The library seems kinda dead sadly maybe I'm using it wrong somehow, but when I tried that it was still returning a double Anyways, I think I'm fine if it's true that doubles are accurate in the scale I'm using them in I appreciate the help, thank you both
cathei
cathei2y ago
Yeah looks like it does double.Parse... I think I would just set the number format on the xlsx as string So it won't read it as number
jr.9
jr.9OP2y ago
sadly not possible, these spreadsheets come in a certain way and have to stay that way
cathei
cathei2y ago
Hmm.. then I would cast it to decimal and then do Math.Round at 2 decimal place, should be fine
jr.9
jr.9OP2y ago
cast it using (Decimal) or Convert.ToDecimal()?
cathei
cathei2y ago
They'll basically do the same
jr.9
jr.9OP2y ago
ah okay thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
jr.9
jr.9OP2y ago
/close
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server