Skip to content

Commit

Permalink
CB-3443 add format number util function (#2263)
Browse files Browse the repository at this point in the history
* CB-3443 add format number util function

* CB-3443 format number from 1k

* CB-3443 include 1k in formatting

---------

Co-authored-by: Evgenia Bezborodova <[email protected]>
  • Loading branch information
devnaumov and EvgeniaBzzz authored Dec 29, 2023
1 parent 5f26449 commit 1d5dc14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions webapp/packages/core-utils/src/formatNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2023 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/

export function formatNumber(n: number, d: number) {
if (n < 1000) {
return n.toString();
}

const numStr = n.toString();
const exponent = numStr.length - (numStr.length % 3);

const power = Math.pow(10, d);
const rounded = Math.round((n * power) / Math.pow(10, exponent)) / power;

const units = ' kMBTPE';
const unit = units[exponent / 3];

return rounded + unit;
}
1 change: 1 addition & 0 deletions webapp/packages/core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export * from './removeMetadataFromBase64';
export * from './renamePathName';
export * from './removeLineBreak';
export * from './replaceSubstring';
export * from './formatNumber';
7 changes: 6 additions & 1 deletion webapp/packages/plugin-d3js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ export {
interpolateRound,
axisBottom,
axisLeft,
scaleOrdinal,
pie,
arc,
schemeTableau10,
sum,
} from 'd3';
export type { Selection, ZoomBehavior, Line, DragBehavior, SubjectPosition } from 'd3';
export type { Selection, ZoomBehavior, Line, DragBehavior, SubjectPosition, PieArcDatum, ScaleOrdinal } from 'd3';

0 comments on commit 1d5dc14

Please sign in to comment.