-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
When working with money (currency) values, it's important to use a data type that preserves exact precision. Consider the following example illustrating the problem of using JavaScript's built-in number type (floating-point numbers):
$ node
> 0.1 + 0.2
0.30000000000000004
We should add a new type allowing users to safely work with Decimal values.
- When storing the value in a database, connectors should use database-specific Decimal type.
- MongoDB has
Decimal128, see support decimal128 loopback-connector-mongodb#475 - PostgreSQL
- Microsoft SQL
- Oracle
- MySQL
- db2
- MongoDB has
- At runtime, it would be great if model data could use a Decimal type too. Unfortunately JavaScript does not provide such a built-in type, but there are npm modules like big.js that can be leveraged as a workaround. Alternatively, we can represent Decimal values as strings for the initial iteration.
- In JSON, decimal values must be represented as strings to avoid loss of precision.
- Code emitting JSON Schema (Swagger, OpenAPI etc.) should indicate that the value type is decimal. Unfortunately, OpenAPI does not seem to support
Decimaltype and while there were some discussions, typically proposing{type: number format: decimal}, most of them ended with no outcome. Since LB4 will be storing decimals in string format, I think we should use{type: string, format: decimal}as the schema for Decimal values.
Reactions are currently unavailable