Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: number is formatted wrong when a dot is a grouping separator #6428

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

sviderock
Copy link
Contributor

@sviderock sviderock commented Jan 21, 2025

Description

When phone number format settings are set to "1.234.567,89" – the number formatting is all over the place. The issue was an unformatNumberForProcessing function which was only expecting numbers that were formatted according to the phone's number format settings while SwapScreenV2 was using numbers sent by a prepare transaction quote which are formatted as a regular JS number ("1234567.89").

This PR fixes this issue.

Test plan

Related issues

N/A

Backwards compatibility

Yes

Network scalability

If a new NetworkId and/or Network are added in the future, the changes in this PR will:

  • Continue to work without code changes, OR trigger a compilation error (guaranteeing we find it when a new network is added)

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.11%. Comparing base (753750b) to head (923f608).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6428      +/-   ##
==========================================
- Coverage   89.12%   89.11%   -0.01%     
==========================================
  Files         735      735              
  Lines       31854    31856       +2     
  Branches     6051     6053       +2     
==========================================
- Hits        28391    28390       -1     
+ Misses       3416     3271     -145     
- Partials       47      195     +148     
Files with missing lines Coverage Δ
src/components/TokenEnterAmount.tsx 95.16% <100.00%> (+0.05%) ⬆️

... and 66 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 753750b...923f608. Read the comment docs.

@sviderock sviderock enabled auto-merge January 21, 2025 11:41
@sviderock sviderock added this pull request to the merge queue Jan 21, 2025
Merged via the queue into main with commit 0406f81 Jan 21, 2025
16 checks passed
@sviderock sviderock deleted the slava/swap-screen-v2-format-fix branch January 21, 2025 12:50
Comment on lines +65 to +67
if (!Number.isNaN(+value)) {
return value.endsWith(groupingSeparator) ? `${+value}` : value
}
Copy link
Member

@jeanregisser jeanregisser Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm not a big fan of the +value coercion trick as it can be confusing to read. I recommend we use BigNumber for number parsing as it's the most predictable and supports arbitrary long numbers with no precision loss.

I think it could be simplified to this:

Suggested change
if (!Number.isNaN(+value)) {
return value.endsWith(groupingSeparator) ? `${+value}` : value
}
const numericValue = new BigNumber(value);
if (!numericValue.isNaN()) {
return numericValue.toFixed()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeanregisser I'll check if that's gonna be a 1 to 1 alternative and if so - will create another PR with it!

github-merge-queue bot pushed a commit that referenced this pull request Feb 3, 2025
…6472)

### Description
This PR again tries to fix an ongoing issue with formatting number for
TokenEnterAmount component when number format in phone settings is set
to `1.234,5678`.
The issue was introduced by [previous
attempt](#6428) to fix wrong
formatting for Swap flow specifically.

Previously, the issue was due to unformatting function only expecting
numbers that are formatted according to the phone's settings. This
caused an issue when on the Swap flow we set the value of "To" textfield
with `replaceAmount` function using the value from the quote API
response which was a JS-compatible number. This format differed for the
users that had format set in settings to `1.234,5678` and caused the
actual issue. But this introduced another issue with the wrong
formatting for when the numbers that exceed `1000`.

In order to fix this, the condition for whether there's a need to skip
the formatting was moved to actual function that is used when replacing
the value with the quote API response. I've also added some explanatory
comments to some functions for better clarity on what's each function
intention is.

### Test plan
Edited e2e tests for Send to test proper formatting of big amounts while
using `typeText` function which simulates actual typing instead of
`replaceText` (which is more of a paste).

This change doesn't revert the previous fix:


https://github.com/user-attachments/assets/a5b913eb-f47c-4a5d-9fcf-1c704376c177

### Related issues

- Relates to [this PR](#6428)
- Relates to the bug mentioned
[here](https://valora-app.slack.com/archives/C04B61SJ6DS/p1738341507462059)

### Backwards compatibility
Yes

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [x] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants