Skip to content

Commit

Permalink
Using one fractional digit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed Jan 30, 2025
1 parent 9db58e5 commit be5c7a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
42 changes: 21 additions & 21 deletions graylog2-web-interface/src/util/NumberFormatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const expectFormattedPercentage = (num: number) => expect(formatPercentage(num /
describe('NumberFormatting', () => {
describe('formatNumber', () => {
it('formats with 2 fraction digits by default', () => {
expectFormattedNumber(42.23).toEqual('42.23');
expectFormattedNumber(42.23).toEqual('42.2');
expectFormattedNumber(42).toEqual('42');
expectFormattedNumber(137.991).toEqual('137.99');
expectFormattedNumber(137.991).toEqual('138');
expectFormattedNumber(137.999).toEqual('138');
expectFormattedNumber(137.111).toEqual('137.11');
expectFormattedNumber(137.115).toEqual('137.12');
expectFormattedNumber(137.111).toEqual('137.1');
expectFormattedNumber(137.115).toEqual('137.1');
expectFormattedNumber(0.23).toEqual('0.23');
});

Expand All @@ -44,36 +44,36 @@ describe('NumberFormatting', () => {

describe('formatTrend', () => {
it('does show sign', () => {
expectFormattedTrend(42.23).toEqual('+42.23');
expectFormattedTrend(42.23).toEqual('+42.2');
expectFormattedTrend(-42).toEqual('-42');
expectFormattedTrend(-137.991).toEqual('-137.99');
expectFormattedTrend(-137.991).toEqual('-138');
expectFormattedTrend(137.999).toEqual('+138');
expectFormattedTrend(-137.111).toEqual('-137.11');
expectFormattedTrend(137.115).toEqual('+137.12');
expectFormattedTrend(-137.111).toEqual('-137.1');
expectFormattedTrend(137.115).toEqual('+137.1');
expectFormattedTrend(0).toEqual('0');
});

it('does show percentage', () => {
const options = { percentage: true };

expectFormattedTrend(42.23 / 100, options).toEqual('+42.23%');
expectFormattedTrend(-42 / 100, options).toEqual('-42.00%');
expectFormattedTrend(-137.991 / 100, options).toEqual('-137.99%');
expectFormattedTrend(137.999 / 100, options).toEqual('+138.00%');
expectFormattedTrend(-137.111 / 100, options).toEqual('-137.11%');
expectFormattedTrend(137.115 / 100, options).toEqual('+137.12%');
expectFormattedTrend(0 / 100, options).toEqual('0.00%');
expectFormattedTrend(42.23 / 100, options).toEqual('+42.2%');
expectFormattedTrend(-42 / 100, options).toEqual('-42.0%');
expectFormattedTrend(-137.991 / 100, options).toEqual('-138.0%');
expectFormattedTrend(137.999 / 100, options).toEqual('+138.0%');
expectFormattedTrend(-137.111 / 100, options).toEqual('-137.1%');
expectFormattedTrend(137.115 / 100, options).toEqual('+137.1%');
expectFormattedTrend(0 / 100, options).toEqual('0.0%');
});
});

describe('formatPercentage', () => {
it('formats with 2 fraction digits by default', () => {
expectFormattedPercentage(42.23).toEqual('42.23%');
expectFormattedPercentage(42).toEqual('42.00%');
expectFormattedPercentage(137.991).toEqual('137.99%');
expectFormattedPercentage(137.999).toEqual('138.00%');
expectFormattedPercentage(137.111).toEqual('137.11%');
expectFormattedPercentage(137.115).toEqual('137.12%');
expectFormattedPercentage(42.23).toEqual('42.2%');
expectFormattedPercentage(42).toEqual('42.0%');
expectFormattedPercentage(137.991).toEqual('138.0%');
expectFormattedPercentage(137.999).toEqual('138.0%');
expectFormattedPercentage(137.111).toEqual('137.1%');
expectFormattedPercentage(137.115).toEqual('137.1%');
expectFormattedPercentage(0.684).toEqual('0.68%');
});

Expand Down
2 changes: 1 addition & 1 deletion graylog2-web-interface/src/util/NumberFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Options = {
minimumDigits?: number,
};

const desiredFractionDigits = 2;
const desiredFractionDigits = 1;

const defaultPercentageOptions = {
minimumFractionDigits: desiredFractionDigits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('TypeSpecificValue', () => {
<TypeSpecificValue field="field1" value={6543.21} unit={FieldUnit.fromJSON({ abbrev: 'ms', unit_type: 'time' })} />,
);

await screen.findByText('6.54 s');
await screen.findByText('6.5 s');
});

it('should render original value if unit not defined', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Trend', () => {
it('shows relative delta as percentage', async () => {
renderTrend({ previous: 23 });

expect(await findTrend()).toMatch(/\+82.61%/);
expect(await findTrend()).toMatch(/\+82.6%/);
});

it('shows absolute delta if values are equal', async () => {
Expand All @@ -53,7 +53,7 @@ describe('Trend', () => {
it('shows relative delta as percentage if values are equal', async () => {
renderTrend();

expect(await findTrend()).toMatch(/0\.00%/);
expect(await findTrend()).toMatch(/0\.0%/);
});

it('shows negative absolute delta', async () => {
Expand All @@ -65,7 +65,7 @@ describe('Trend', () => {
it('shows negative relative delta as percentage', async () => {
renderTrend({ current: 23 });

expect(await findTrend()).toMatch(/-45.24%/);
expect(await findTrend()).toMatch(/-45.2%/);
});

it('shows adequate results if previous value is 0', async () => {
Expand All @@ -83,7 +83,7 @@ describe('Trend', () => {
it('shows adequate results if current value is 0', async () => {
renderTrend({ current: 0, previous: 42 });

expect(await findTrend()).toMatch(/-42 \/ -100\.00%/);
expect(await findTrend()).toMatch(/-42 \/ -100\.0%/);
});

it('shows adequate results if current value is NaN', async () => {
Expand Down

0 comments on commit be5c7a4

Please sign in to comment.