Skip to content

Commit 8a33c18

Browse files
committed
Perform floor division when explicitly converting to bytes
1 parent df2a9be commit 8a33c18

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

README.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ binary
55
:target: https://pypi.org/project/binary
66
:alt: Latest PyPI version
77

8-
.. image:: https://github.com/ofek/binary/workflows/test/badge.svg
8+
.. image:: https://github.com/ofek/binary/actions/workflows/test.yml/badge.svg
99
:target: https://github.com/ofek/binary/actions/workflows/test.yml
1010
:alt: GitHub Actions
1111

@@ -17,10 +17,14 @@ binary
1717
:target: https://pypi.org/project/binary
1818
:alt: Supported Python versions
1919

20-
.. image:: https://img.shields.io/pypi/l/binary.svg?style=flat-square
21-
:target: https://choosealicense.com/licenses
20+
.. image:: https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-9400d3.svg
21+
:target: https://spdx.org/licenses/
2222
:alt: License
2323

24+
.. image:: https://img.shields.io/github/sponsors/ofek?logo=GitHub%20Sponsors&style=social
25+
:target: https://github.com/sponsors/ofek
26+
:alt: GitHub sponsors
27+
2428
-----
2529

2630
``binary`` provides a bug-free and easy way to convert between and within
@@ -32,10 +36,6 @@ binary (`IEC`_) and decimal (`SI`_) units.
3236
Installation
3337
------------
3438

35-
``binary`` is distributed on `PyPI <https://pypi.org>`_ as a universal
36-
wheel and is available on Linux/macOS and Windows and supports
37-
Python 2.7/3.5+ and PyPy.
38-
3939
.. code-block:: bash
4040
4141
$ pip install binary
@@ -163,6 +163,11 @@ Changelog
163163

164164
Important changes are emphasized.
165165

166+
1.0.2
167+
^^^^^
168+
169+
- Perform floor division when explicitly converting to bytes
170+
166171
1.0.1
167172
^^^^^
168173

binary/core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ def convert_units(
165165

166166
if to:
167167
try:
168-
return b / to, PREFIXES[to]
168+
if to == BYTE:
169+
return b // to, PREFIXES[to]
170+
else:
171+
return b / to, PREFIXES[to]
169172
except KeyError:
170-
raise ValueError(f'{to} is not a valid binary unit.')
173+
raise ValueError(f'{to} is not a valid unit.')
171174

172175
if unit in BINARY_PREFIXES and not si:
173176
if b < KIBIBYTE:

tests/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def test_yottabyte(self) -> None:
173173

174174
class TestConvertFloatExact:
175175
def test_byte(self) -> None:
176-
assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392.64'), 'B')
177-
assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000.00'), 'B')
176+
assert convert_units(3.14, bunits.YB, bunits.B, exact=True) == (Decimal('3796027073589935608577392'), 'B')
177+
assert convert_units(3.14, dunits.YB, dunits.B, exact=True) == (Decimal('3140000000000000000000000'), 'B')
178178

179179
def test_kibibyte(self) -> None:
180180
assert convert_units(3.14, bunits.YB, bunits.KB, exact=True) == (Decimal('3707057689052671492751.36'), 'KiB')

0 commit comments

Comments
 (0)