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

[HOLD for payment 2023-10-05] [$500] [Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point #27107

Closed
6 tasks
kbecciv opened this issue Sep 10, 2023 · 32 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor

Comments

@kbecciv
Copy link

kbecciv commented Sep 10, 2023

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


Action Performed:

  1. Change the rate of a workspace to 1.283
  2. Request money -> Distance
  3. Fill start and finish point
  4. Click next and select that workspace

Expected Result:

the rate in the Distance field should be 1.283

Actual Result:

the rate in the Distance field: 1.283000000000...

Workaround:

Unknown

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android / native
  • Android / Chrome
  • iOS / native
  • iOS / Safari
  • MacOS / Chrome / Safari
  • MacOS / Desktop

Version Number: 1.3.66.3
Reproducible in staging?: y
Reproducible in production?: y
If this was caught during regression testing, add the test name, ID and link from TestRail:
Email or phone of affected tester (no customers):
Logs: https://stackoverflow.com/c/expensify/questions/4856
Notes/Photos/Videos: Any additional supporting documentation

0000

Recording.4363.mp4

Expensify/Expensify Issue URL:
Issue reported by: @hichamcc
Slack conversation: https://expensify.slack.com/archives/C049HHMV9SM/p1693912703643649

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~010a1acc02586756aa
  • Upwork Job ID: 1700872084741091328
  • Last Price Increase: 2023-09-10
  • Automatic offers:
    • samh-nl | Contributor | 26728259
@kbecciv kbecciv added External Added to denote the issue can be worked on by a contributor Daily KSv2 labels Sep 10, 2023
@DylanDylann
Copy link
Contributor

Proposal

Please re-state the problem that we are trying to solve in this issue.

[Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point

What is the root cause of that problem?

The result of this calculation is wrong

const ratePerUnit = rate * 0.01;

This is a problem of javascript like this

What changes do you think we should make in order to solve the problem?

Using decimal.js (this lib is used in package-lock.json) like this

const ratePerUnit = new Decimal(rate).dividedBy(100).toNumber()

What alternative solutions did you explore? (Optional)

We also can round ratePerUnit by using the toFixed method of js with the number of digits after the comma

    const ratePerUnit = (rate * 0.01).toFixed(3); // need to confirm the number of digits after the comma

@ahmedGaber93
Copy link
Contributor

Proposal

Please re-state the problem that we are trying to solve in this issue.

[Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point

What is the root cause of that problem?

This is the expected result in js when multiply point values see here for more details

console.log(0.1 + 0.2)         ->  will print 0.30000000000000004 not 0.3

What changes do you think we should make in order to solve the problem?

We need to use ratePerUnit.toFixed(3) here

const getDistanceMerchant = (distanceInMeters, unit, rate, currency, translate) => {
const convertedDistance = convertDistanceUnit(distanceInMeters, unit);
const distanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers');
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer');
const roundedDistance = convertedDistance.toFixed(2);
const unitString = roundedDistance === 1 ? singularDistanceUnit : distanceUnit;
const ratePerUnit = rate * 0.01;
const currencySymbol = CurrencyUtils.getCurrencySymbol(currency) || `${currency} `;
return `${roundedDistance} ${unitString} @ ${currencySymbol}${ratePerUnit} / ${singularDistanceUnit}`;
};

What alternative solutions did you explore? (Optional)

we can use third party library like js-big-decimal.js or decimal.js

@namhihi237
Copy link
Contributor

Proposal

Please re-state the problem that we are trying to solve in this issue.

Error displaying rate when requesting money

What is the root cause of that problem?

We use the function getDistanceMerchant to get distance merchant

const getDistanceMerchant = (distanceInMeters, unit, rate, currency, translate) => {
const convertedDistance = convertDistanceUnit(distanceInMeters, unit);
const distanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers');
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer');
const roundedDistance = convertedDistance.toFixed(2);
const unitString = roundedDistance === 1 ? singularDistanceUnit : distanceUnit;
const ratePerUnit = rate * 0.01;
const currencySymbol = CurrencyUtils.getCurrencySymbol(currency) || `${currency} `;
return `${roundedDistance} ${unitString} @ ${currencySymbol}${ratePerUnit} / ${singularDistanceUnit}`;
};

The problem with this line

const ratePerUnit = rate * 0.01;

When performing the calculation 128.3 * 0.01 in JavaScript, the returned result is not 1.283, but 1.2830000000000001. This comes from the way computers handle decimals in the floating-point system.

What changes do you think we should make in order to solve the problem?

To control the number of decimal places after the decimal point:

const ratePerUnit = (rate * 0.01).toFixed(3);

What alternative solutions did you explore? (Optional)

If we want to remove unnecessary zeros after the decimal point:

const ratePerUnit = Number((rate * 0.01).toFixed(3));

Or

const ratePerUnit = Number((rate * 0.01));

@kbecciv kbecciv added the Bug Something is broken. Auto assigns a BugZero manager. label Sep 10, 2023
@samh-nl
Copy link
Contributor

samh-nl commented Sep 10, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

Some rates in the distance field have an excessive number of zeros added after the decimal point

What is the root cause of that problem?

In DistanceRequestUtils.getDistanceMerchant, we multiply the value by 0.01, causing the extra decimals due to it being a floating point:

const ratePerUnit = rate * 0.01;

In other instances where the rate is displayed, we use getRateDisplayValue and getNumericValue to get the rate and use toFixed(3) to prevent this problem:

Furthermore, there are other inconsistencies. For example, here we multiply by 0.01, but in the other implementations we divide by CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET (value of 100).

What changes do you think we should make in order to solve the problem?

We currently have 3 implementations for getting the rate value, with getRateDisplayValue (and getNumericValue) being essentially duplicated in the two files mentioned above.

We should move getRateDisplayValue to a util file and use the same function everwhere (i.e. DistanceRequestUtils, WorkspaceRateAndUnitPage and WorkspaceRateAndUnitPage).

This would solve the issue and also make the implementations consistent.

Fixed result:

image

What alternative solutions did you explore? (Optional)

Option 1. Add toFixed(3), but this would be a less elegant approach.
Option 2. Use a library such as decimal.js for all money-related amounts.

@melvin-bot melvin-bot bot changed the title [Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point [$500] [Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point Sep 10, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 10, 2023

Job added to Upwork: https://www.upwork.com/jobs/~010a1acc02586756aa

@melvin-bot
Copy link

melvin-bot bot commented Sep 10, 2023

Triggered auto assignment to @trjExpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Sep 10, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 10, 2023

Bug0 Triage Checklist (Main S/O)

  • This "bug" occurs on a supported platform (ensure Platforms in OP are ✅)
  • This bug is not a duplicate report (check E/App issues and #expensify-bugs)
    • If it is, comment with a link to the original report, close the issue and add any novel details to the original issue instead
  • This bug is reproducible using the reproduction steps in the OP. S/O
    • If the reproduction steps are clear and you're unable to reproduce the bug, check with the reporter and QA first, then close the issue.
    • If the reproduction steps aren't clear and you determine the correct steps, please update the OP.
  • This issue is filled out as thoroughly and clearly as possible
    • Pay special attention to the title, results, platforms where the bug occurs, and if the bug happens on staging/production.
  • I have reviewed and subscribed to the linked Slack conversation to ensure Slack/Github stay in sync

@melvin-bot
Copy link

melvin-bot bot commented Sep 10, 2023

Triggered auto assignment to @strepanier03 (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.

@melvin-bot
Copy link

melvin-bot bot commented Sep 10, 2023

Triggered auto assignment to Contributor-plus team member for initial proposal review - @burczu (External)

@trjExpensify
Copy link
Contributor

@hayata-suenaga putting this one on your radar and adding it to the project board!

image

@burczu
Copy link
Contributor

burczu commented Sep 12, 2023

Reviewed all the proposals and all of them are correct in my opinion - I personally prefer just using the toFixed method here. @DylanDylann's proposal was first, so I think we should choose it.
But he proposed using library as a main solution and using toFixed as optional one. I'm not sure if it does matter in terms of choosing proposal - if it does, then the first proposal to choose is the one from @ahmedGaber93.

🎀 👀 🎀 C+ reviewed

@melvin-bot
Copy link

melvin-bot bot commented Sep 12, 2023

Triggered auto assignment to @luacmartins, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

@samh-nl
Copy link
Contributor

samh-nl commented Sep 12, 2023

@burczu In both WorkspaceRateAndUnitPage and WorkspaceReimburseView we use the getRateDisplayValue function which does not have this issue. For consistency, why not apply the same method everywhere (rather than duplicating code)?

There's also another difference in implementation: * 0.01 (hard-coded value) is done here instead of dividing by 100 (CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET) that is done in getRateDisplayValue, which seems like the intended way of displaying the rate.

@DylanDylann
Copy link
Contributor

@samh-nl I see that it is the same idea using toFixed method to avoid this issue

@luacmartins
Copy link
Contributor

hmm I think using getRateDisplayValue might be a better solution in this case. It seems like we already use that method in a few places, so maybe we should extract it into its own util method too. What do you think @burczu?

@burczu
Copy link
Contributor

burczu commented Sep 13, 2023

@luacmartins Yes, I agree.

@melvin-bot melvin-bot bot added the Overdue label Sep 15, 2023
@luacmartins
Copy link
Contributor

In that case it seems like we should go with @samh-nl's proposal.

@melvin-bot melvin-bot bot added Overdue and removed Overdue labels Sep 15, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 18, 2023

📣 @hichamcc We're missing your Upwork ID to automatically send you an offer for the Reporter role.
Once you apply to the Upwork job, your Upwork ID will be stored and you will be automatically hired for future jobs!

@luacmartins
Copy link
Contributor

Assigned @samh-nl to the issue

@melvin-bot melvin-bot bot removed the Overdue label Sep 18, 2023
@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Daily KSv2 labels Sep 19, 2023
@samh-nl
Copy link
Contributor

samh-nl commented Sep 19, 2023

PR is ready for review: #27816

@melvin-bot
Copy link

melvin-bot bot commented Sep 25, 2023

Based on my calculations, the pull request did not get merged within 3 working days of assignment. Please, check out my computations here:

  • when @samh-nl got assigned: 2023-09-18 08:43:47 Z
  • when the PR got merged: 2023-09-25 02:55:47 UTC
  • days elapsed: 4

On to the next one 🚀

@melvin-bot melvin-bot bot added Weekly KSv2 Awaiting Payment Auto-added when associated PR is deployed to production and removed Weekly KSv2 labels Sep 28, 2023
@melvin-bot melvin-bot bot changed the title [$500] [Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point [HOLD for payment 2023-10-05] [$500] [Distance] Some rates in the distance field have an excessive number of zeros added after the decimal point Sep 28, 2023
@melvin-bot melvin-bot bot removed the Reviewing Has a PR in review label Sep 28, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 28, 2023

Reviewing label has been removed, please complete the "BugZero Checklist".

@melvin-bot
Copy link

melvin-bot bot commented Sep 28, 2023

The solution for this issue has been 🚀 deployed to production 🚀 in version 1.3.74-3 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

If no regressions arise, payment will be issued on 2023-10-05. 🎊

After the hold period is over and BZ checklist items are completed, please complete any of the applicable payments for this issue, and check them off once done.

  • External issue reporter
  • Contributor that fixed the issue
  • Contributor+ that helped on the issue and/or PR

For reference, here are some details about the assignees on this issue:

As a reminder, here are the bonuses/penalties that should be applied for any External issue:

  • Merged PR within 3 business days of assignment - 50% bonus
  • Merged PR more than 9 business days after assignment - 50% penalty

@melvin-bot
Copy link

melvin-bot bot commented Sep 28, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@burczu] The PR that introduced the bug has been identified. Link to the PR:
  • [@burczu] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:
  • [@burczu] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:
  • [@burczu] Determine if we should create a regression test for this bug.
  • [@burczu] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
  • [@trjExpensify] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Oct 5, 2023
@trjExpensify
Copy link
Contributor

👋 @burczu can you complete the checklist please so we can proceed with payments? Thanks!

@melvin-bot melvin-bot bot added the Overdue label Oct 9, 2023
@melvin-bot
Copy link

melvin-bot bot commented Oct 9, 2023

@burczu, @trjExpensify, @luacmartins, @samh-nl Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

@trjExpensify
Copy link
Contributor

Same, awaiting the checklist.

@burczu
Copy link
Contributor

burczu commented Oct 10, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@burczu] The PR that introduced the bug has been identified. Link to the PR: Modify MoneyRequestConfirmPage for Distance Request #25707
  • [@burczu] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment: Modify MoneyRequestConfirmPage for Distance Request #25707 (comment)
  • [@burczu] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion: Didn't find any
  • [@burczu] Determine if we should create a regression test for this bug.
  • [@burczu] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
  • [@trjExpensify] Link the GH issue for creating/updating the regression test once above steps have been agreed upon: Don't think we need this as the default rate is three decimals for a workspace, so it will reveal itself in the standard creation flow.

@trjExpensify
Copy link
Contributor

Okay, confirming payments on this one as follows:

Sent offers! Once confirmed, I'll pay them out.

@hichamcc
Copy link

@trjExpensify accepted

@trjExpensify
Copy link
Contributor

settled up with you both. Closing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor
Projects
None yet
Development

No branches or pull requests

10 participants