Skip to content

Commit 0a83e2b

Browse files
committed
📝 Update docs for decimals, include explanation and examples
1 parent f3ee8b9 commit 0a83e2b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/advanced/decimal.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Pydantic has special support for `Decimal` types using the <a href="https://pyda
2626

2727
But meanwhile, you can already use this feature with `condecimal()` in **SQLModel** it as it's explained here.
2828

29-
When you use `condecimal()` you can specify the number of decimal places and digits to support. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
29+
When you use `condecimal()` you can specify the number of digits and decimal places to support. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
3030

3131
!!! info
3232
For the database, **SQLModel** will use <a href="https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.DECIMAL" class="external-link" target="_blank">SQLAlchemy's `DECIMAL` type</a>.
@@ -50,6 +50,31 @@ Let's say that each hero in the database will have an amount of money. We could
5050

5151
</details>
5252

53+
Here we are saying that `money` can have at most `5` digits with `max_digits`, **this includes the integers** (to the left of the decimal dot) **and the decimals** (to the right of the decimal dot).
54+
55+
We are also saying that the number of decimal places (to the right of the decimal dot) is `3`, so we can have **3 decimal digits** for these numbers in the `money` field. This means that we will have **2 digits for the integer part** and **3 digits for the decimal part**.
56+
57+
✅ So, for example, these are all valid numbers for the `money` field:
58+
59+
* `12.345`
60+
* `12.3`
61+
* `12`
62+
* `1.2`
63+
* `0.123`
64+
* `0`
65+
66+
🚫 But these are all invalid numbers for that `money` field:
67+
68+
* `1.2345`
69+
* This number has more than 3 decimal places.
70+
* `123.234`
71+
* This number has more than 5 digits in total (integer and decimal part).
72+
* `123`
73+
* Even though this number doesn't have any decimals, we still have 3 places saved for them, which means that we can **only use 2 places** for the **integer part**, and this number has 3 integer digits. So, the allowed number of integer digits is `max_digits` - `decimal_places` = 2.
74+
75+
!!! tip
76+
Make sure you adjust the number of digits and decimal places for your own needs, in your own application. 🤓
77+
5378
## Create models with Decimals
5479

5580
When creating new models you can actually pass normal (`float`) numbers, Pydantic will automatically convert them to `Decimal` types, and **SQLModel** will store them as `Decimal` types in the database (using SQLAlchemy).

0 commit comments

Comments
 (0)