Skip to content

Commit 2018b6c

Browse files
Add NumberFormat framework
1 parent 9222168 commit 2018b6c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/frameworks/number-format.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ nav:
6161
- DownloadManager: frameworks/download-manager.md
6262
- InputManager: frameworks/input-manager.md
6363
- LightsManager: frameworks/lights-manager.md
64+
- NumberFormat: frameworks/number-format.md
6465
- Preferences: frameworks/preferences.md
6566
- SensorManager: frameworks/sensor-manager.md
6667
- SettingActivity: frameworks/setting-activity.md

0 commit comments

Comments
 (0)