Repository navigation
fix: localize the decimal separator in scientific-notation results too - #140
Open
bluzername wants to merge 1 commit into
Open
bluzername wants to merge 1 commit into
bluzername wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong
`format_number` in `src/plugins/unit_converter.rs` has two branch: the normal decimal branch, and a scientific-notation branch for value below `1e-6` or at/above `1e12`. Both branch are supposed to respect the locale's decimal separator (`uses_decimal_comma`), the comment above the function even say so ("babel.numbers.format_decimal").
But the scientific branch `return\`s before the `uses_decimal_comma` check:
```rust
if magnitude != 0.0 && !(1e-6..1e12).contains(&magnitude) {
return format!("{value:e}"); // early return, never reach the comma swap below
}
let mut text = format!("{value:.10}");
...
if uses_decimal_comma(locale) {
text = text.replace('.', ",");
}
```
So for a "de"/"fr"/... locale, a normal-range answer like `1.5` become `1,5` (correct), but a very big or very small answer like `1.5e13` stays `1.5e13` with the English dot, not `1,5e13`. This is inconsistent inside the same function, same feature, same answer even.
This is reachable from real queries, for example converting a huge distance from millimeter to kilometer, or very small mass unit, can push the SI value past `1e12` or below `1e-6`.
Fix
Apply the exact same `uses_decimal_comma` replace already used two line below, to the scientific branch too, before returning.
Added `scientific_notation_still_uses_the_locale_decimal_separator`: checks a huge value (`1.5e13`) with `"de"` and a tiny one (`2.5e-7`) with `"fr"` both get a comma and no dot, while `"en"` keep the dot (`"1.5e13"`), which match Rust's `{:e}` output for that exact value.
Checks
Same limitation as my other PR on this repo today: I did not have execution access to run `cargo fmt`/`cargo test`/`cargo clippy` in this environment, so I am not claiming they passed. I traced `format_number`'s two branches by hand side by side and I am confident the fix mirror the existing, already-tested behavior of the branch right below it, only applied to the other branch too. Please run the usual `cargo fmt --all --check`, `cargo test --all-targets`, `cargo clippy --all-targets -- -D warnings` on CI before merge.