Skip to content

Commit

Permalink
Introduce c specifier for float formatting
Browse files Browse the repository at this point in the history
Behaves like g, but tries to compact value for fixed precision before comparing with exponential
  • Loading branch information
linev committed Sep 17, 2024
1 parent 6a5f570 commit f0dd069
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions modules/base/BasePainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ function floatToString(value, fmt, ret_fmt, significance) {
fmt = '6.4g';
else if (fmt === 'g')
fmt = '8.6g';
else if (fmt === 'c')
fmt = '8.6c';

fmt = fmt.trim();
const len = fmt.length;
Expand All @@ -104,9 +106,18 @@ function floatToString(value, fmt, ret_fmt, significance) {
case 'f':
isexp = false;
break;
case 'c':
case 'g': {
const se = floatToString(value, fmt+'e', true, true);
let sg = floatToString(value, fmt+'f', true, true);
const pnt = sg[0].indexOf('.');
if ((kind === 'c') && (pnt > 0)) {
let len = sg[0].length;
while ((len > pnt) && (sg[0][len-1] === '0'))
len--;
if (len === pnt) len--;
sg[0] = sg[0].slice(0, len);
}
if (se[0].length < sg[0].length) sg = se;
return ret_fmt ? sg : sg[0];
}
Expand Down
2 changes: 1 addition & 1 deletion modules/gpad/TAxisPainter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const AxisPainterMethods = {
return res;
}

return floatToString(val, fmt || 'g');
return floatToString(val, fmt || 'c');
},

/** @summary Provide label for exponential form */
Expand Down

0 comments on commit f0dd069

Please sign in to comment.