Skip to content

Commit

Permalink
Simplify logic for rounding 2 digit decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
joko3ono committed Dec 5, 2024
1 parent 91ab49d commit 3d9b939
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 26 deletions.
2 changes: 1 addition & 1 deletion public/css/app.min.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/js/tests/visualisation_helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('tick_formatter', () => {
test('formats tick for amino_acid sequence', () => {
const formatter = tick_formatter(null, 'amino_acid');
expect(formatter(1_000)).toBe('1 kaa');
expect(formatter(25_286_936)).toBe('25.2 Maa');
expect(formatter(25_286_936)).toBe('25.3 Maa');
});

test('formats tick for nucleic_acid sequence', () => {
Expand All @@ -18,7 +18,7 @@ describe('tick_formatter', () => {
describe('formatNumberUnits', () => {
test('formats number with units correctly', () => {
expect(formatNumberUnits('1.9k')).toBe('1.9 k');
expect(formatNumberUnits('1.5k', 0.5)).toBe('2 k');
expect(formatNumberUnits('1.5k')).toBe('1.5 k');
});

test('rounds up correctly based on threshold', () => {
Expand Down
25 changes: 6 additions & 19 deletions public/js/visualisation_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,13 @@ export function tick_formatter(scale, seq_type) {
};
}

function extractSuffix(str) {
const suffix = str.replace(/[0-9.]/g, '');
const numericPart = d3.format('.2f')(parseFloat(str));
export function formatNumberUnits(number) {
const suffix = number.replace(/[0-9.]/g, '');
const numericPart = parseFloat(number).toFixed(2);
// Round to 1 decimal place
const rounded = Math.round(numericPart * 10) / 10;

return [parseFloat(numericPart), suffix];
}

export function formatNumberUnits(number, threshold = 0.95) {
const [numericPart, suffix] = extractSuffix(number);
const integerPart = Math.floor(numericPart);
const fractionalPart = parseFloat((numericPart - integerPart).toFixed(2));

// when fractionalPart more than the threshold round up
// example:
// threshold 0.95
// 2.95 -> 3
// 2.94 -> 2.9
const formatted = fractionalPart >= threshold ? integerPart + 1 : Math.floor(numericPart * 10) / 10;

return `${formatted} ${suffix}`;
return `${rounded % 1 === 0 ? rounded.toFixed(0) : rounded} ${suffix}`;
}

export function get_seq_type(algorithm) {
Expand Down
2 changes: 1 addition & 1 deletion public/sequenceserver-report.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sequenceserver-report.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sequenceserver-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sequenceserver-search.min.js.map

Large diffs are not rendered by default.

0 comments on commit 3d9b939

Please sign in to comment.