Skip to content

Commit

Permalink
Merge pull request #13 from smartbooster/change_format_byte_1000
Browse files Browse the repository at this point in the history
`MathUtils::formatBytes` : Use 1000 instead of 1024 for calculate
  • Loading branch information
mathieu-ducrot authored Jan 11, 2024
2 parents b1e4f18 + 0472db3 commit b13ff37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
CHANGELOG for 1.x
===================
## v1.0.3 - (2024-01-11)

### Fix

- `MathUtils::formatBytes` : Use 1000 instead of 1024 for calculate

## v1.0.2 - (2024-01-11)

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/MathUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function calculatePercentage(?float $partial, float $total, ?int $
*/
public static function formatBytes(float $size, int $precision = 2): string
{
$base = log($size, 1024);
$base = log($size, 1000);
$suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];

$floor = floor($base);
return round(pow(1024, $base - $floor), $precision) . ' ' . $suffixes[$floor];
return round(pow(1000, $base - $floor), $precision) . ' ' . $suffixes[$floor];
}
}
8 changes: 4 additions & 4 deletions tests/Utils/MathUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public function formatBytesProvider(): array
// precision
null,
],
'1.26 KB' => ['1.26 KB', 1290, null],
'536.918 MB' => ['536.918 MB', 562999000, 3],
'880 GB' => ['880 GB', 945000000000, 0],
'382.86 TB' => ['382.86 TB', 420956000000000, 2],
'1.29 KB' => ['1.29 KB', 1290, null],
'562.999 MB' => ['562.999 MB', 562999000, 3],
'945 GB' => ['945 GB', 945000000000, 0],
'420.96 TB' => ['420.96 TB', 420956000000000, 2],
];
}
}

0 comments on commit b13ff37

Please sign in to comment.