Skip to content

Commit

Permalink
fix: improve modal calculator long floating point amounts (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seavenly committed Apr 4, 2023
1 parent bc12e53 commit 37e10e1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/modal/v2/lib/hooks/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ export default function useCalculator({ autoSubmit = false } = {}) {
// If we see new offer terms, which match the amount prop, but the value in the input does not match
// This means the amount changed outside the modal, so we update the offer terms
// we want to update the inputValue, so force autoSubmit: false
if (viewWithOffersAmount === amount && delocalize(state.inputValue, country) !== amount) {
if (
// Instead of comparing equality, check for a small delta difference of a cent since the amount
// provided could be a value resulted from JavaScript math where the value has floating point
// precision issues (e.g. 100.000000000001)
Math.abs(viewWithOffersAmount - amount) < 0.01 &&
Math.abs(delocalize(state.inputValue, country) - amount) >= 0.01
) {
dispatch({
type: 'view',
data: {
Expand Down

0 comments on commit 37e10e1

Please sign in to comment.