fix(axes): format tick labels correctly for small numbers in exponential notation#7768
fix(axes): format tick labels correctly for small numbers in exponential notation#7768Hasnaathussain wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
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.
| 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+$/, ''); | ||
| } | ||
| } |
| 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+$/, ''); | ||
| } |
| v = String(v); | ||
| var dp = v.indexOf('.') + 1; | ||
| if(dp) v = v.slice(0, dp + tickRound).replace(/\.?0+$/, ''); | ||
| 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+$/, ''); | ||
| } |
There was a problem hiding this comment.
What do you think of replacing most of this with a call to toFixed? The exponent portion should get added further down on line 2240 (in your branch).
|
@camdecoster Applied! Replaced the custom mantissa slicing with a clean oFixed call. |
|
There was some funkiness with adding the rounding increment, so I made a few changes. Now the tests are passing! Speaking of which, could you please add some tests that check this new approach? Also, could you please add a draftlog? |
What this PR does
This PR fixes a bug in
src/plots/cartesian/axes.jswhere formatting very small numbers could result in invalid tick strings (e.g."-9.381779999999999e-") whenString(v)produces exponential notation but thenumFormatlogic 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-jasmineto ensure no existing suites are broken.