Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

fix: localized base-10 data size units #2207

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void main() {
),
);
expect(
find.widgetWithText(SpinBox, (5 * 1024).toString()), findsOneWidget);
find.widgetWithText(SpinBox, (5 * 1000).toString()), findsOneWidget);
});

testWidgets('in kilobytes', (tester) async {
Expand All @@ -94,7 +94,7 @@ void main() {
onUnitSelected: (_) {},
),
);
expect(find.widgetWithText(SpinBox, (5 * 1024 * 1024).toString()),
expect(find.widgetWithText(SpinBox, (5 * 1000 * 1000).toString()),
findsOneWidget);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/ubuntu_utils/lib/src/data_size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dart:math' as math;
enum DataUnit { bytes, kilobytes, megabytes, gigabytes }

int toBytes(double size, DataUnit unit) {
return (size * math.pow(1024, unit.index)).round();
return (size * math.pow(1000, unit.index)).round();
}

double fromBytes(int size, DataUnit unit) {
return size / math.pow(1024, unit.index).toInt();
return size / math.pow(1000, unit.index).toInt();
}
12 changes: 6 additions & 6 deletions packages/ubuntu_utils/test/data_size_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ void main() {
});

test('kilobytes', () {
expect(toBytes(1, DataUnit.kilobytes), equals(1024));
expect(fromBytes(1024, DataUnit.kilobytes), equals(1));
expect(toBytes(1, DataUnit.kilobytes), equals(1000));
expect(fromBytes(1000, DataUnit.kilobytes), equals(1));
});

test('megabytes', () {
expect(toBytes(1, DataUnit.megabytes), equals(1024 * 1024));
expect(fromBytes(1024 * 1024, DataUnit.megabytes), equals(1));
expect(toBytes(1, DataUnit.megabytes), equals(1000 * 1000));
expect(fromBytes(1000 * 1000, DataUnit.megabytes), equals(1));
});

test('bytes', () {
expect(toBytes(1, DataUnit.gigabytes), equals(1024 * 1024 * 1024));
expect(fromBytes(1024 * 1024 * 1024, DataUnit.gigabytes), equals(1));
expect(toBytes(1, DataUnit.gigabytes), equals(1000 * 1000 * 1000));
expect(fromBytes(1000 * 1000 * 1000, DataUnit.gigabytes), equals(1));
});
}