Skip to content

fix: localize the decimal separator in scientific-notation results too - #140

Open
bluzername wants to merge 1 commit into
sahenjp:mainfrom
bluzername:fix/scientific-notation-locale-comma
Open

bluzername wants to merge 1 commit into
sahenjp:mainfrom
bluzername:fix/scientific-notation-locale-comma

Conversation

@bluzername

Copy link
Copy Markdown

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.

@bluzername
bluzername requested a review from sahenjp as a code owner November 14, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant