Skip to content

Commit

Permalink
Number formatting according to locale
Browse files Browse the repository at this point in the history
This proposal implements the formatting numbers with the proper thousand separator and decimal sign according to the locale for the `.read_formatted()` function.

To use this feature, a module should at first
`
    import locale
    locale.setlocale(locale.LC_ALL, '')
`
and then call `.read_formatted()` with the named parameter `use_locale` set to `True`, for example: `read_formatted(register, use_locale=True)`.
  • Loading branch information
hchiper authored Apr 3, 2024
1 parent 162216b commit 74cc685
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sun2000_modbus/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ def read(self, register):
else:
return raw_value / register.value.gain

def read_formatted(self, register):
def read_formatted(self, register, use_locale=False): # added the last argument.
value = self.read(register)

if register.value.unit is not None:
return f'{value} {register.value.unit}'
if use_locale:
return f'{value:n} {register.value.unit}' # added :n to format number according to the locale.
else:
return f'{value} {register.value.unit}'
elif register.value.mapping is not None:
return register.value.mapping.get(value, 'undefined')
else:
Expand Down

0 comments on commit 74cc685

Please sign in to comment.