Skip to content

Add a built-in Decimal/Numeric type #1902

@bajtos

Description

@bajtos

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.
  • 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 Decimal type 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions