Skip to content

Commit 40334d5

Browse files
committed
Refactor rounding logic; don't apply rounding increment to value
1 parent bc032db commit 40334d5

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

src/plots/cartesian/axes.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,9 +2175,6 @@ function numFormat(v, ax, fmtoverride, hover) {
21752175

21762176
if(tickformat) return ax._numFormat(tickformat)(v).replace(/-/g, MINUS_SIGN);
21772177

2178-
// 'epsilon' - rounding increment
2179-
var e = Math.pow(10, -tickRound) / 2;
2180-
21812178
// exponentFormat codes:
21822179
// 'e' (1.2e+6, default)
21832180
// 'E' (1.2E+6)
@@ -2192,26 +2189,26 @@ function numFormat(v, ax, fmtoverride, hover) {
21922189
// take the sign out, put it back manually at the end
21932190
// - makes cases easier
21942191
v = Math.abs(v);
2192+
2193+
// 'epsilon' - rounding increment
2194+
const e = Math.pow(10, -tickRound) / 2;
21952195
if(v < e) {
21962196
// 0 is just 0, but may get exponent if it's the last tick
21972197
v = '0';
21982198
isNeg = false;
21992199
} else {
2200-
v += e;
22012200
// take out a common exponent, if any
22022201
if(exponent) {
22032202
v *= Math.pow(10, -exponent);
22042203
tickRound += exponent;
22052204
}
22062205
// round the mantissa
2207-
if(tickRound === 0) v = String(Math.floor(v));
2208-
else if(tickRound < 0) {
2206+
if(tickRound === 0) {
22092207
v = String(Math.round(v));
2210-
v = v.slice(0, Math.max(0, v.length + tickRound));
2211-
for(var i = tickRound; i < 0; i++) v += '0';
2208+
} else if(tickRound < 0) {
2209+
const roundingMagnitude = Math.pow(10, -tickRound);
2210+
v = String(Math.round(v / roundingMagnitude) * roundingMagnitude);
22122211
} else {
2213-
// subtract the half-epsilon added above so toFixed doesn't double-round
2214-
v -= e;
22152212
v = v.toFixed(Math.min(20, tickRound)).replace(/\.?0+$/, '');
22162213
}
22172214
// insert appropriate decimal point and thousands separator

0 commit comments

Comments
 (0)