Skip to content

Commit 102bb99

Browse files
committed
fix(Data Storage Units Converter): fix b/iB/B conversion
Fix #398
1 parent dfb92f2 commit 102bb99

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/tools/data-storage-unit-converter/data-storage-unit-converter.service.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ describe('data-storage-unit-converter', () => {
2929
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'Mb', toUnit: 'KB', precision: 0 })).toBe('125');
3030
expect(convertStorageAndRateUnitsDisplay({ value: 125, fromUnit: 'KB', toUnit: 'Mb', precision: 0 })).toBe('1');
3131

32+
expect(convertStorageAndRateUnitsDisplay({ value: 8, fromUnit: 'b', toUnit: 'B' })).toBe('1.000');
33+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'B', toUnit: 'b' })).toBe('8.000');
34+
expect(convertStorageAndRateUnitsDisplay({ value: 8000, fromUnit: 'b', toUnit: 'KB' })).toBe('1.000');
35+
expect(convertStorageAndRateUnitsDisplay({ value: 1000, fromUnit: 'iB', toUnit: 'b' })).toBe('8000.000');
36+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'KiB', toUnit: 'b' })).toBe('8192.000');
37+
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'KB', toUnit: 'iB' })).toBe('1000.000');
38+
3239
expect(convertStorageAndRateUnitsDisplay({ value: 1, fromUnit: 'MiB', toUnit: 'Kb' })).toBe('8388.608');
3340
expect(convertStorageAndRateUnitsDisplay({ value: 8388.608, fromUnit: 'Kb', toUnit: 'MiB', precision: 0 })).toBe('1');
3441
});

src/tools/data-storage-unit-converter/data-storage-unit-converter.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ export function convertStorageAndRateUnits(
3232
];
3333

3434
const fromIndex = units.indexOf(fromUnit);
35-
const fromFactor = fromIndex / 9 > 1 ? 1000 : 1024;
36-
const fromDivisor = fromIndex / 9 > 2 ? 8 : 1;
35+
const fromFactor = fromIndex / 9 >= 1 ? 1000 : 1024;
36+
const fromDivisor = fromIndex / 9 >= 2 ? 8 : 1;
3737
const toIndex = units.indexOf(toUnit);
38-
const toFactor = toIndex / 9 > 1 ? 1000 : 1024;
39-
const toDivisor = toIndex / 9 > 2 ? 8 : 1;
38+
const toFactor = toIndex / 9 >= 1 ? 1000 : 1024;
39+
const toDivisor = toIndex / 9 >= 2 ? 8 : 1;
4040

4141
const fromBase = (fromFactor ** (fromIndex % 9)) / fromDivisor;
4242
const toBase = (toFactor ** (toIndex % 9)) / toDivisor;

0 commit comments

Comments
 (0)