Skip to content

Commit 6c37b75

Browse files
Merge pull request #25 from james-smith-za/master
Add ability to use separator to EngNumber
2 parents 93c8bc7 + a090117 commit 6c37b75

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

engineering_notation/engineering_notation.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class EngUnit:
4545
Represents an engineering number, complete with units
4646
"""
4747
def __init__(self, value,
48-
precision=2, significant=0, unit: str = None):
48+
precision=2, significant=0, unit: str = None, separator=""):
4949
"""
5050
Initialize engineering with units
5151
:param value: the desired value in the form of a string, int, or float
@@ -70,10 +70,10 @@ def __init__(self, value,
7070
if self.unit is None and len(value) >= v_index:
7171
self.unit = value[v_index:]
7272

73-
self.eng_num = EngNumber(new_value, precision, significant)
73+
self.eng_num = EngNumber(new_value, precision, significant, separator)
7474

7575
else:
76-
self.eng_num = EngNumber(value, precision, significant)
76+
self.eng_num = EngNumber(value, precision, significant, separator)
7777

7878
def __repr__(self):
7979
"""
@@ -282,7 +282,7 @@ class EngNumber:
282282
"""
283283

284284
def __init__(self, value,
285-
precision=2, significant=0):
285+
precision=2, significant=0, separator=""):
286286
"""
287287
Initialize the class
288288
@@ -294,6 +294,7 @@ def __init__(self, value,
294294
"""
295295
self.precision = precision
296296
self.significant = significant
297+
self.separator = separator
297298

298299
if isinstance(value, str):
299300
suffix_keys = [key for key in _suffix_lookup.keys() if key != '']
@@ -332,7 +333,7 @@ def to_pn(self, sub_letter=None):
332333
return string
333334

334335
letter = string[-1]
335-
return string.replace('.', letter)[:-1]
336+
return string.replace('.', letter)[:-1].strip(self.separator)
336337

337338
def __repr__(self):
338339
"""
@@ -375,7 +376,7 @@ def __repr__(self):
375376
if '.00' in base:
376377
base = base[:-3]
377378

378-
return base + _exponent_lookup_scaled[exponent]
379+
return base + self.separator + _exponent_lookup_scaled[exponent]
379380

380381
def __str__(self, eng=True, context=None):
381382
"""

tests/test_engnum.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def test_engnum_significant():
7070
assert str(EngNumber('2.22m', significant=3)) == '2.22m'
7171

7272

73+
def test_engnum_separator():
74+
assert str(EngNumber("1.23k", separator=" ")) == "1.23 k"
75+
assert str(EngNumber("1.23k", separator=" ").to_pn()) == "1k23"
76+
77+
7378
def test_new_units():
7479
"""
7580
any new units - such as femto, atto, zepto, etc. should have
@@ -304,10 +309,12 @@ def test_to_str():
304309
# positive_numbers
305310
assert str(EngUnit('220')) == '220'
306311
assert str(EngUnit('220ohm')) == '220ohm'
312+
assert str(EngUnit('220ohm', separator=" ")) == '220 ohm'
307313

308314
# negative_numbers
309315
assert str(EngUnit('-220')) == '-220'
310316
assert str(EngUnit('-220ohm')) == '-220ohm'
317+
assert str(EngUnit('-220ohm', separator=" ")) == '-220 ohm'
311318

312319
assert EngUnit('220ohm').unit == 'ohm'
313320
assert EngUnit('220', unit='ohm').unit == 'ohm'

0 commit comments

Comments
 (0)