diff --git a/frontend/src/app/main/ui/workspace/sidebar/branch_review/common.cljs b/frontend/src/app/main/ui/workspace/sidebar/branch_review/common.cljs index cf978891c0..e3c0b021cf 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/branch_review/common.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/branch_review/common.cljs @@ -190,11 +190,15 @@ (defn- fmt-number "At most two decimals, trailing zeros stripped (89.99999 -> \"90\", 439.4999 -> \"439.5\", 386 -> \"386\"), grouped and punctuated the way - the active locale writes numbers (\"439,5\" in Spanish)." + the active locale writes numbers (\"439,5\" in Spanish). It goes + through `Intl.NumberFormat`: once a plugin has loaded, the SES lockdown + turns `Number.prototype.toLocaleString` into `toString`, options and + all." [v] - (let [locale (-> i18n/*current-locale* - (str/replace "_" "-"))] - (.toLocaleString v locale #js {:maximumFractionDigits 2}))) + (let [locale (or (some-> i18n/*current-locale* + (str/replace "_" "-")) + js/undefined)] + (.format (js/Intl.NumberFormat. locale #js {:maximumFractionDigits 2}) v))) (defn- short-str [v] diff --git a/frontend/test/frontend_tests/ui/branches_compare_labels_test.cljs b/frontend/test/frontend_tests/ui/branches_compare_labels_test.cljs index f5363d4c72..4fe8f2058c 100644 --- a/frontend/test/frontend_tests/ui/branches_compare_labels_test.cljs +++ b/frontend/test/frontend_tests/ui/branches_compare_labels_test.cljs @@ -69,3 +69,17 @@ (t/is (= "439.5" (display-val 439.5))) (t/is (= "90" (display-val 89.99999)) "at most two decimals") (t/is (= "386" (display-val 386))))) + +(t/deftest numbers-keep-two-decimals-under-the-plugin-lockdown + ;; a loaded plugin's SES lockdown turns `toLocaleString` into + ;; `toString`, which ignores the options + (let [proto (.-prototype js/Number) + original (.-toLocaleString proto)] + (set! (.-toLocaleString proto) (.-toString proto)) + (try + (binding [i18n/*current-locale* "en"] + (t/is (= "163" (display-val 162.99999952316284)))) + (binding [i18n/*current-locale* nil] + (t/is (= "1.5" (display-val 1.5)) "no locale falls back to the default")) + (finally + (set! (.-toLocaleString proto) original)))))