Skip to content
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

feat(NODE-5594): add Decimal128.fromStringWithRounding() static method #617

Merged
merged 13 commits into from
Sep 7, 2023

Conversation

W-A-James
Copy link
Contributor

@W-A-James W-A-James commented Aug 30, 2023

Description

What is changing?

  • Adds Decimal128.fromStringWithRounding static method
  • Convert test/node/decimal128_tests.js to typescript and add tests for fromStringWithRounding
Is there new documentation needed for these changes?
  • Yes, DOCSP ticket will be filed

What is the motivation for this change?

NODE-5594

Release Highlight

Adds new Decimal128.fromStringWithRounding static method

Prior to the 6.0.0 release, Decimal128.fromString performed rounding behaviour by default that rounded numbers with >34 significant digits incorrectly. In addition to this bug, the intention of the Decimal128 type and specification is to provide an interface to a high-precision floating point value, so rounding by default also went against this intention, which is why that behaviour was removed from the method in the 6.0.0 release.

We understand that some users may have come to rely on the rounding behaviour, so that has been corrected here and exposed via a new static method, Decimal128.fromStringWithRounding, which should now allow these users to migrate to the v6 of the driver.

Thank you to @hconn-riparian for reporting a related rounding bug and fix in #560 which has been included in this feature.

// 5.x
> let d = Decimal128.fromString('127341286781293491234791234667890123')
new Decimal128("1.273412867812934912347912346678901E+35")

// 6.x
> let d = Decimal128.fromString('127341286781293491234791234667890123')
Uncaught:
BSONError: "127341286781293491234791234667890123" is not a valid Decimal128 string - inexact rounding
    at invalidErr (./js-bson/lib/bson.cjs:1402:11)
    at Decimal128.fromStringInternal (./js-bson/lib/bson.cjs:1633:25)
    at Decimal128.fromString (./js-bson/lib/bson.cjs:1424:27)

> d = Decimal128.fromStringWithRounding('127341286781293491234791234667890123')
new Decimal128("1.273412867812934912347912346678901E+35")

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@W-A-James W-A-James changed the title fix(NODE-5594): wip - pre cherry pick feat(NODE-5594): add Decimal128.fromStringWithRounding() static method Aug 30, 2023
@W-A-James W-A-James mentioned this pull request Aug 31, 2023
5 tasks
@W-A-James W-A-James marked this pull request as ready for review August 31, 2023 18:08
@durran durran self-assigned this Aug 31, 2023
@durran durran added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Aug 31, 2023
test/node/decimal128.test.ts Outdated Show resolved Hide resolved
test/node/decimal128.test.ts Outdated Show resolved Hide resolved
test/node/decimal128.test.ts Outdated Show resolved Hide resolved
test/node/decimal128.test.ts Outdated Show resolved Hide resolved
src/decimal128.ts Outdated Show resolved Hide resolved
@W-A-James
Copy link
Contributor Author

@durran durran added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Sep 6, 2023
@durran durran merged commit 6fee2d5 into main Sep 7, 2023
@durran durran deleted the NODE-5594 branch September 7, 2023 20:37
W-A-James added a commit that referenced this pull request Sep 8, 2023
W-A-James added a commit that referenced this pull request Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants