From 8834a19743197e0c227c614b42b3ea6d56246cda Mon Sep 17 00:00:00 2001 From: ilyasosman Date: Mon, 10 Jun 2024 22:50:35 +0300 Subject: [PATCH] Denominations on tokens --- frontend/src/settings/Documents.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/frontend/src/settings/Documents.tsx b/frontend/src/settings/Documents.tsx index e73ce73f..19bde1fa 100644 --- a/frontend/src/settings/Documents.tsx +++ b/frontend/src/settings/Documents.tsx @@ -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 = ({ documents, handleDeleteDocument, @@ -40,7 +58,7 @@ const Documents: React.FC = ({ {document.date} - {document.tokens ? document.tokens : ''} + {document.tokens ? formatTokens(+document.tokens) : ''} {document.location === 'remote'