New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a built-in Decimal/Numeric type #1902

Open
bajtos opened this Issue Oct 23, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@bajtos
Member

bajtos commented Oct 23, 2018

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.
@hacksparrow

This comment has been minimized.

Member

hacksparrow commented Oct 23, 2018

Much needed type. Also, sometimes people want to literally store 1.0 and not 1.

@raymondfeng

This comment has been minimized.

Member

raymondfeng commented Oct 23, 2018

@bajtos bajtos added the major label Nov 5, 2018

@bajtos

This comment has been minimized.

Member

bajtos commented Nov 5, 2018

When working with money (currency) values, it's important to use a data type that preserves exact precision. We should add a new type allowing users to safely work with Decimal values.

@strongloop/loopback-next I'd like to hear your opinion. Is Decimal type something you would need in the projects you are building? If yes then please upvote this issue by pressing the thumbs-up button at the top, just under the issue description.

@BostjanPisler

This comment has been minimized.

BostjanPisler commented Nov 5, 2018

We work with financial numbers and calculation in LB3 on a daily basis and in my opinion storing and retrieving decimal numbers is not a problem, the problem is, as stated above, in working with them ie. floating point calculations. We use decimal.js to calculate and then convert back to normal JS number for storing and retrieval.

@niyoko

This comment has been minimized.

Member

niyoko commented Nov 5, 2018

There are 3 options that we can use: big.js, bignumber.js and decimal.js
The differences are discussed here.

big.js: minimalist; easy-to-use; precision specified in decimal places; precision applied to division only.
bignumber.js: bases 2-64; configuration options; NaN; Infinity; precision specified in decimal places; precision applied to division only; base prefixes.
decimal.js: bases 2-64; configuration options; NaN; Infinity; non-integer powers, exp, ln, log; precision specified in significant digits; precision always applied; random numbers.

Currently I use decimal.js, but for most projects, I think bignumber.js is sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment