From f4e2aef475386bccf5d65eb1eb5c87161f8e1c1c Mon Sep 17 00:00:00 2001 From: ShareVB Date: Wed, 11 Sep 2024 23:30:37 +0200 Subject: [PATCH] fix: display numbers always with dot --- components.d.ts | 2 ++ .../data-storage-unit-converter.service.test.ts | 14 +++++++------- .../data-storage-unit-converter.service.ts | 4 +--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/components.d.ts b/components.d.ts index 8e59366ab..96649b118 100644 --- a/components.d.ts +++ b/components.d.ts @@ -64,6 +64,8 @@ declare module '@vue/runtime-core' { 'CTextCopyable.demo': typeof import('./src/ui/c-text-copyable/c-text-copyable.demo.vue')['default'] CTooltip: typeof import('./src/ui/c-tooltip/c-tooltip.vue')['default'] 'CTooltip.demo': typeof import('./src/ui/c-tooltip/c-tooltip.demo.vue')['default'] + DataStorageUnitConverter: typeof import('./src/tools/data-storage-unit-converter/data-storage-unit-converter.vue')['default'] + DataTransferRateConverter: typeof import('./src/tools/data-transfer-rate-converter/data-transfer-rate-converter.vue')['default'] DateTimeConverter: typeof import('./src/tools/date-time-converter/date-time-converter.vue')['default'] 'DemoHome.page': typeof import('./src/ui/demo/demo-home.page.vue')['default'] DemoWrapper: typeof import('./src/ui/demo/demo-wrapper.vue')['default'] diff --git a/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts b/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts index d65385e4e..32786478d 100644 --- a/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts +++ b/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts @@ -6,10 +6,10 @@ describe('data-storage-unit-converter', () => { it('convert from same base units', () => { expect(convertStorageAndRateUnitsDisplay({ value: 1024 * 1024, fromUnit: 'B', toUnit: 'MiB' })).toBe('1'); expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KiB', toUnit: 'MiB' })).toBe('1'); - expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1,024'); + expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'KiB' })).toBe('1024'); expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1'); - expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1,024'); - expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1,000'); + expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MB', toUnit: 'MB' })).toBe('1024'); + expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'KB' })).toBe('1000'); expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'MiB', toUnit: 'GiB' })).toBe('1'); expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MB', toUnit: 'GB' })).toBe('1'); expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'Mb', toUnit: 'Gb' })).toBe('1'); @@ -20,16 +20,16 @@ describe('data-storage-unit-converter', () => { expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1.049'); expect(convertStorageAndRateUnitsDisplay({ value: 1000 * 1000, fromUnit: 'B', toUnit: 'MiB' })).toBe('0.954'); expect(convertStorageAndRateUnitsDisplay({ value: 1024, fromUnit: 'KB', toUnit: 'MiB' })).toBe('0.977'); - expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1,048.576'); + expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'MiB', toUnit: 'MB' })).toBe('1048.576'); expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MB', toUnit: 'Mb' })).toBe('8'); - expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8,000'); - expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8,192'); + expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KB', toUnit: 'Kb' })).toBe('8000'); + expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'KiB', toUnit: 'Kb' })).toBe('8192'); expect(convertStorageAndRateUnitsDisplay({ value: 8, fromUnit: 'Mb', toUnit: 'MB' })).toBe('1'); expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'Mb', toUnit: 'KB' })).toBe('125'); expect(convertStorageAndRateUnitsDisplay({ value: 125, fromUnit: 'KB', toUnit: 'Mb' })).toBe('1'); - expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8,388.608'); + expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8388.608'); expect(convertStorageAndRateUnitsDisplay({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB' })).toBe('1'); }); it('convert with unit display', () => { diff --git a/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts b/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts index f93634969..3a52f1e95 100644 --- a/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts +++ b/src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts @@ -6,9 +6,7 @@ export type AllSupportedUnits = BibytesUnits | BytesUnits | BitsUnits; export function displayStorageAndRateUnits( { value, unit, precision = 3, appendUnit = false }: { value: number; unit: AllSupportedUnits; precision?: number ; appendUnit?: boolean }): string { - return value.toLocaleString(undefined, { - maximumFractionDigits: precision, - }) + (appendUnit ? unit : ''); + return value.toFixed(precision).replace(/0+$/, '').replace(/\.$/, '') + (appendUnit ? unit : ''); // NO SONAR } export function convertStorageAndRateUnitsDisplay(