|
| 1 | +# NumberFormat |
| 2 | + |
| 3 | +The NumberFormat framework provides a small utility for formatting numbers using the system's number format preference stored in SharedPreferences. |
| 4 | + |
| 5 | +## Preferences |
| 6 | + |
| 7 | +The format preference is stored in `SharedPreferences` under: |
| 8 | + |
| 9 | +- App ID: `com.micropythonos.settings` |
| 10 | +- Key: `number_format` |
| 11 | + |
| 12 | +If the key is missing, the default format is `comma_dot`. |
| 13 | + |
| 14 | +## Supported formats |
| 15 | + |
| 16 | +The available formats are: |
| 17 | + |
| 18 | +- `comma_dot`: `1,234.56` (US/UK) |
| 19 | +- `dot_comma`: `1.234,56` (Europe) |
| 20 | +- `space_comma`: `1 234,56` (French) |
| 21 | +- `apos_dot`: `1'234.56` (Swiss) |
| 22 | +- `under_dot`: `1_234.56` (Tech) |
| 23 | +- `none_dot`: `1234.56` (no thousands separator) |
| 24 | +- `none_comma`: `1234,56` (no thousands separator) |
| 25 | + |
| 26 | +## API |
| 27 | + |
| 28 | +### `NumberFormat.refresh_preference()` |
| 29 | + |
| 30 | +Reloads the preference from SharedPreferences. |
| 31 | + |
| 32 | +### `NumberFormat.get_separators()` |
| 33 | + |
| 34 | +Returns a tuple `(decimal_sep, thousands_sep)` for the current preference. |
| 35 | + |
| 36 | +### `NumberFormat.format_number(value, decimals=None)` |
| 37 | + |
| 38 | +Formats a number using the current preference. |
| 39 | + |
| 40 | +- `value`: `int` or `float` |
| 41 | +- `decimals`: number of decimal places |
| 42 | + - `None` (default): |
| 43 | + - `int` values are formatted without decimals |
| 44 | + - `float` values are formatted to 2 decimals, then trailing zeros are stripped |
| 45 | + |
| 46 | +### `NumberFormat.get_format_options()` |
| 47 | + |
| 48 | +Returns a list of `(label, key)` tuples for use in settings dropdowns. |
| 49 | + |
| 50 | +## Example |
| 51 | + |
| 52 | +```python |
| 53 | +from mpos import NumberFormat |
| 54 | + |
| 55 | +NumberFormat.refresh_preference() |
| 56 | + |
| 57 | +print(NumberFormat.format_number(1234567)) # 1,234,567 (depends on preference) |
| 58 | +print(NumberFormat.format_number(1234.5)) # 1,234.5 (depends on preference) |
| 59 | +print(NumberFormat.format_number(1234.5, 3)) # 1,234.500 (depends on preference) |
| 60 | +``` |
0 commit comments