❔ 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
Any particular reason you'd need to do that? It's safer to work with currency using decimals all in all
I don't have a choice, excel stores numbers as doubles
ah, sorry, I misunderstood your issue
you're fine, I appreciate you taking the time to look at it
Can't you read it as
string
?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
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...
Doubles should be decently accurate, as long as you're not working with thousands of values with millionth parts of a cent each
Ty
Nah, two decimal points at most and mostly just tens of dollars
Might as well just work with cents then, ig
Multiply each value by 100 and floor it to an integer
Zero rounding error possibility
I thought decimal was safe to work with?
It is, yeah
That's another possibility, sure
follow up question:
Should I typecast it or use Convert.ToDecimal()?
No luck with turning off
UseColumnDataType
?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
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 numbersadly not possible, these spreadsheets come in a certain way and have to stay that way
Hmm.. then I would cast it to decimal and then do
Math.Round
at 2 decimal place, should be finecast it using (Decimal) or Convert.ToDecimal()?
They'll basically do the same
ah okay
thanks
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./close
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.