Skip to content

Commit

Permalink
Merge pull request #990 from IlyasOsman/token-format
Browse files Browse the repository at this point in the history
Denominations on tokens
  • Loading branch information
dartpain committed Jun 11, 2024
2 parents 9862083 + 8834a19 commit 3df745d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion frontend/src/settings/Documents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import { DocumentsProps } from '../models/misc';
import Trash from '../assets/trash.svg';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';

// Utility function to format numbers
const formatTokens = (tokens: number): string => {
const roundToTwoDecimals = (num: number): string => {
return (Math.round((num + Number.EPSILON) * 100) / 100).toString();
};

if (tokens >= 1_000_000_000) {
return roundToTwoDecimals(tokens / 1_000_000_000) + 'b';
} else if (tokens >= 1_000_000) {
return roundToTwoDecimals(tokens / 1_000_000) + 'm';
} else if (tokens >= 1_000) {
return roundToTwoDecimals(tokens / 1_000) + 'k';
} else {
return tokens.toString();
}
};

const Documents: React.FC<DocumentsProps> = ({
documents,
handleDeleteDocument,
Expand Down Expand Up @@ -40,7 +58,7 @@ const Documents: React.FC<DocumentsProps> = ({
{document.date}
</td>
<td className="border-r border-t px-4 py-2">
{document.tokens ? document.tokens : ''}
{document.tokens ? formatTokens(+document.tokens) : ''}
</td>
<td className="border-r border-t px-4 py-2">
{document.location === 'remote'
Expand Down

0 comments on commit 3df745d

Please sign in to comment.