Skip to content

Commit

Permalink
Fix the humanReadableText function to support number between 0 and 1
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 570825705
  • Loading branch information
lionelfeng authored and copybara-github committed Oct 4, 2023
1 parent b68d688 commit 8d4dfa1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions frontend/app/common/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export function humanReadableText(
const units = si ? ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] :
['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
const i = num === 0 ? 0 : Math.floor(Math.log(num) / Math.log(base));
return (Number(num / Math.pow(base, i)).toFixed(dp) + ' ' + units[i] +
suffix) ||
'';
// Handle the case if num < 1 (e.g. 0.000123)
// we should return 1.23e-4 for better readability.
const n = 0 < num && num < 1 ? num.toExponential(dp) :
Number(num / Math.pow(base, i)).toFixed(dp);
return (n + ' ' + units[Math.max(0, i)] + suffix) || '';
}

/**
Expand Down

0 comments on commit 8d4dfa1

Please sign in to comment.