-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
29.02 cell cast to 29.020000000000003 string #289
Comments
Hello @severinh In cell.as_string() Even with cell.get_float().map(|v| v.to_string()) or cell.as_f64() we get the same behavior with this floating precision issue and AFAIK there is no way for us to get more info on the original input. EDIT: quick check on calamine inner: [
SharedString(
"Decimals",
),
Float(
28.14,
),
Float(
29.020000000000003,
),
], when reading the xml content we get BytesText { content: Borrowed("29.020000000000003") } so I guess it goes even further directly in the xml content 😞 |
The floating point number in the Excel file is 29.020000000000003: <row r="2" spans="1:1" x14ac:dyDescent="0.2">
<c r="A2" s="1">
<v>28.14</v>
</c>
</row>
<row r="3" spans="1:1" x14ac:dyDescent="0.2">
<c r="A3" s="1">
<v>29.020000000000003</v>
</c>
</row> For the display (and storage) of floating point numbers Excel uses something very similar to the $ python
>>> print("%.16G" % 29.020000000000003)
29.02 That format option isn't supported by Rust in std::fmt however. |
Thanks @jmcnamara indeed it's really in the xml content itself. Maybe we could use With [dependencies]
rust_decimal = { version = "1.36.0", default-features = false } use rust_decimal::prelude::*;
fn main() {
let x = 29.020000000000003_f64;
println!("x: {}", Decimal::from_f64(x).unwrap());
} ▶ cargo r -q
x: 29.02 WDYT? |
I was wrong about this for the display part. Excel displays decimal numbers with 8 digits in a standard cell width and 10 digits in a wide cell. Like this:
It also trims trailing zeros and the decimal point if there are no fractional part. |
Again thank you for the info. Much appreciated. |
Huge thanks for the deep investigation into this issue, @PrettyWood and @jmcnamara! Learned a lot from reading this discussion. I would trust you to make the right decisions if and where this should be changed. I also hope that this won't have any negative impact on other users of |
We chose to support it directly in fastexcel for now. It should have very little impact because it's only applied for float to string conversion |
Steps to reproduce
See the Excel file decimal-numbers.xlsx. It contains a column with two decimals:
Read the Excel file using fastexcel:
Looks fine.
Then read the Excel file while casting to strings:
The expected result was that the strings would be the same as shown to the user in the Excel file:
Wrap-up
I understand this looks like an issue due to floating point precision, and I'm not sure if this:
What's the motivation for filing this bug: In our system, we have highly heterogeneous data, so we have to read all Excel values as strings. However, if users see 29.02 in their Excel files, but
29.020000000000003
in our system, that's highly confusing and surprising to users.What do you think?
The text was updated successfully, but these errors were encountered: