-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Use Math.round() instead of Math.trunc() to fix floating-point precision issues #18816
Conversation
@dangrous @thesahindia One of you needs to copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
@therealsujitk, please add the screen recording for all the platforms and also checkoff all the checkboxes. |
@thesahindia I've updated my post with screen recordings. Unfortunately I can't test on iOS native and desktop as I don't have access to a Macbook. |
@therealsujitk do you think you could add a couple test cases to the existing test, too, to cover the weird amounts? App/tests/unit/CurrencyUtilsTest.js Lines 88 to 99 in 9fa8c89
|
@dangrous yeah sure! Edit: I'll take a look at why the test case failed as well. |
I went ahead and deleted a duplicate test case ( |
tests/unit/CurrencyUtilsTest.js
Outdated
[CONST.CURRENCY.USD, 25, 2500], | ||
[CONST.CURRENCY.USD, 25.5, 2550], | ||
[CONST.CURRENCY.USD, 25.5, 2550], | ||
[CONST.CURRENCY.USD, 80.6, 8060], | ||
[CONST.CURRENCY.USD, 80.9, 8090], | ||
[CONST.CURRENCY.USD, 80.99, 8099], | ||
['JPY', 25, 25], | ||
['JPY', 2500, 2500], | ||
['JPY', 25.5, 25], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what this test was trying to do with that doubled test was to copy the same test cases in both USD and JPY, and something just got lost along the way. Here's my suggestion here:
[CONST.CURRENCY.USD, 25, 2500],
[CONST.CURRENCY.USD, 25.5, 2550],
[CONST.CURRENCY.USD, 2500, 250000],
[CONST.CURRENCY.USD, 80.6, 8060],
[CONST.CURRENCY.USD, 80.9, 8090],
[CONST.CURRENCY.USD, 80.99, 8099],
['JPY', 25, 25],
['JPY', 2500, 2500],
['JPY', 25.5, 25],
['JPY', 80.6, 80],
['JPY', 80.9, 80],
['JPY', 80.99, 80],
My only question here is whether or not this would be expected behavior for the JPY cases. Would we really expect 80.99 to come in as 80? cc @roryabraham in case you happen to know. If you don't, I think we stick with it because that isn't changing any behavior from what exists now...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding JPY, somewhere in the comments this issue was linked: #15878
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, that is a LONG chain. I THINK, if I'm understanding it correctly, that we actually DO want to round up 80.99 to 81 because that's what we'd display:
I think most of the floor/trunc logic is only when we're splitting things, so we wouldn't want to also do it here.
But I'll cc @aldo-expensify since he worked on that issue and maybe he can confirm one way or the other - do we want to round up/down decimals in currencies without decimals, or should we always truncate/floor them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand everything going on here... but I would ask first why are we testing conversions of decimal JPY. Is it possible for a user to input JPY with decimal positions? if it is not, maybe then these test are testing things that can't happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, this is not necessarily related to currencies like JPY, but this instead:
Number.parseFloat('80.60') * 100
8059.999999999999
.. yeah the trunc result is definitely wrong:
Math.trunc(80.6 * 100);
8059
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that question too actually, just checked and yes, you can enter decimal for JPN currently.
Unrelated to this issue but it would be better to prevent users from doing that IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code changes make sense.
Regarding JPY, I wouldn't care much about that for now. I'm guessing that some code in the backend could produce JPY decimals... but 🤷
Reviewer Checklist
Screenshots/VideosWebScreen.Recording.2023-05-12.at.8.14.23.PM.movMobile Web - ChromeScreen.Recording.2023-05-12.at.8.52.51.PM.movMobile Web - SafariScreen.Recording.2023-05-12.at.8.55.56.PM.movDesktopScreen.Recording.2023-05-12.at.9.00.35.PM.moviOSScreen.Recording.2023-05-12.at.8.53.50.PM.movAndroidScreen.Recording.2023-05-12.at.8.20.25.PM.mov |
@therealsujitk, you will need to checkoff all the items in the "PR author checklist" ![]() |
I think it's fine for one PR. However, I want to inform you that it's necessary to provide screenshots for all platforms, which requires access to a Mac. Other option is to use cloud services (more info). Please refrain from applying to any new job until this matter is resolved. |
Done 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well!
@dangrous, let's rerun the checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
🚀 Deployed to staging by https://github.com/dangrous in version: 1.3.15-0 🚀
|
🚀 Deployed to production by https://github.com/yuwenmemon in version: 1.3.15-12 🚀
|
Details
I've changed
Math.trunc()
to useMath.round()
to fix floating-point precision issues. This will work for numbers for more decimal places in case support for that is added in future (Unless the number of decimal places is incredibly large which will never be the case).Fixed Issues
$ #18688
PROPOSAL: #18688 (comment)
Tests
80.60
,80.90
,80.99
).Offline tests
QA Steps
80.60
,80.90
,80.99
).PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Web
Screencast.from.10-05-23.09.34.41.AM.IST.webm
Mobile Web - Chrome
screen-20230512-154832.mp4
Mobile Web - Safari
WhatsApp.Video.2023-05-12.at.15.58.11.mp4
Desktop
iOS
Android
screen-20230512-162625.mp4