Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Handling of maximum precision is inconsistent between decimal and scientific notation #704

Open
janlugt opened this issue Feb 24, 2025 · 0 comments

Comments

@janlugt
Copy link

janlugt commented Feb 24, 2025

I ran into this issue when parsing JSON with floats that are sometimes written in scientific notation. When a float exceeds the maximum scale, it is simply cut off by rust_decimal when it's written in decimal notation, but in scientific notation, an error is generated. To reproduce:

#[derive(serde::Deserialize)]
struct F {
    f: rust_decimal::Decimal
}

// Simply parsing as a `serde_json::Value` works fine.
assert!(serde_json::from_str::<serde_json::Value>(
    r#"{ "f": 1E-29 }"#,
).is_ok());

// Parsing the same number in decimal notation to a `rust_decimal::Decimal` is fine too.
assert!(serde_json::from_str::<F>(
    r#"{ "f": 0.00000000000000000000000000001 }"#,
).is_ok());

// But parsing it when written in scientific notation fails.
assert!(serde_json::from_str::<F>(
    r#"{ "f": 1E-29 }"#,
).is_err());
@janlugt janlugt changed the title Handling of maximum scale is inconsistent between decimal and scientific notation Handling of maximum precision is inconsistent between decimal and scientific notation Feb 24, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant