Skip to content

Commit 7832e59

Browse files
committed
Fix d3#1369 - localized decimal point.
The old implementation was broken because string.replace returns a new string rather than modifying the string in-place (obviously, because strings are immutable), and was further broken because the localized thousands separator can be "." in some locales. This new implementation breaks the value into separate integer and decimal parts to avoid such confusion.
1 parent 5fd964b commit 7832e59

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

d3.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,11 +2021,12 @@ d3 = function() {
20212021
value *= scale;
20222022
}
20232023
value = type(value, precision);
2024-
if (!zfill && comma) value = d3_format_group(value);
2025-
var length = base.length + value.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : "";
2026-
if (zcomma) value = d3_format_group(padding + value);
2027-
if (d3_format_decimalPoint) value.replace(".", d3_format_decimalPoint);
2024+
var i = value.lastIndexOf("."), before = i < 0 ? value : value.substring(0, i), after = i < 0 ? "" : d3_format_decimalPoint + value.substring(i + 1);
2025+
if (!zfill && comma) before = d3_format_group(before);
2026+
var length = base.length + before.length + after.length + (zcomma ? 0 : negative.length), padding = length < width ? new Array(length = width - length + 1).join(fill) : "";
2027+
if (zcomma) before = d3_format_group(padding + before);
20282028
negative += base;
2029+
value = before + after;
20292030
return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + suffix;
20302031
};
20312032
};
@@ -2069,13 +2070,12 @@ d3 = function() {
20692070
if (d3_format_grouping) {
20702071
var d3_format_groupingLength = d3_format_grouping.length;
20712072
d3_format_group = function(value) {
2072-
var i = value.lastIndexOf("."), f = i >= 0 ? "." + value.substring(i + 1) : (i = value.length,
2073-
""), t = [], j = 0, g = d3_format_grouping[0];
2073+
var i = value.length, t = [], j = 0, g = d3_format_grouping[0];
20742074
while (i > 0 && g > 0) {
20752075
t.push(value.substring(i -= g, i + g));
20762076
g = d3_format_grouping[j = (j + 1) % d3_format_groupingLength];
20772077
}
2078-
return t.reverse().join(d3_format_thousandsSeparator || "") + f;
2078+
return t.reverse().join(d3_format_thousandsSeparator);
20792079
};
20802080
}
20812081
d3.geo = {};

0 commit comments

Comments
 (0)