Skip to content

fix(axes): format tick labels correctly for small numbers in exponential notation#7768

Open
Hasnaathussain wants to merge 1 commit intoplotly:masterfrom
Hasnaathussain:fix-tick-formatting-exponential
Open

fix(axes): format tick labels correctly for small numbers in exponential notation#7768
Hasnaathussain wants to merge 1 commit intoplotly:masterfrom
Hasnaathussain:fix-tick-formatting-exponential

Conversation

@Hasnaathussain
Copy link
Copy Markdown

What this PR does

This PR fixes a bug in src/plots/cartesian/axes.js where formatting very small numbers could result in invalid tick strings (e.g. "-9.381779999999999e-") when String(v) produces exponential notation but the numFormat logic slices the string indiscriminately.

Fix details

When exponential notation is present, the logic now separates the mantissa from the exponent, applies precision formatting to the mantissa, and re-attaches the exponent string, avoiding string truncation that removes the exponential component.

Tested against locally failing reproductions and verified using test-jasmine to ensure no existing suites are broken.

Copilot AI review requested due to automatic review settings April 22, 2026 19:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes tick label formatting in numFormat for very small numbers where String(v) returns exponential notation, preventing invalid/truncated tick strings.

Changes:

  • Detects exponential notation from String(v) and splits mantissa/exponent before applying rounding/truncation.
  • Re-attaches the exponent after formatting the mantissa to avoid slicing into the exponent.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2220 to +2226
var adjustedTickRound = tickRound + exponentVal;
if(dp) {
if(adjustedTickRound < 0) {
mantissa = mantissa.slice(0, dp - 1);
} else {
mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, '');
}
} else {
mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, '');
}
}
Comment on lines +2213 to +2233
var vStr = String(v);
var ep = vStr.indexOf('e');
if(ep >= 0) {
var mantissa = vStr.slice(0, ep);
var exponentStr = vStr.slice(ep);
var dp = mantissa.indexOf('.') + 1;
var exponentVal = parseInt(exponentStr.slice(1), 10);
var adjustedTickRound = tickRound + exponentVal;
if(dp) {
if(adjustedTickRound < 0) {
mantissa = mantissa.slice(0, dp - 1);
} else {
mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, '');
}
}
v = mantissa + exponentStr;
} else {
v = vStr;
var dp = v.indexOf('.') + 1;
if(dp) v = v.slice(0, dp + tickRound).replace(/\.?0+$/, '');
}
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.

2 participants