Skip to content

Commit

Permalink
Fix number input on mobile with hidden decimals (#4503)
Browse files Browse the repository at this point in the history
* Fix number input on mobile with hidden decimals

* Add release notes
  • Loading branch information
jfdoming authored Mar 2, 2025
1 parent 8900627 commit e10b105
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
66 changes: 65 additions & 1 deletion packages/loot-core/src/shared/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util';
import {
looselyParseAmount,
getNumberFormat,
setNumberFormat,
currencyToAmount,
} from './util';

describe('utility functions', () => {
test('looseParseAmount works with basic numbers', () => {
Expand Down Expand Up @@ -108,4 +113,63 @@ describe('utility functions', () => {
formatter = getNumberFormat().formatter;
expect(formatter.format(Number('1234.56'))).toBe('1’235');
});

test('currencyToAmount works with basic numbers', () => {
expect(currencyToAmount('3')).toBe(3);
expect(currencyToAmount('3.4')).toBe(3.4);
expect(currencyToAmount('3.45')).toBe(3.45);
expect(currencyToAmount('3.45060')).toBe(3.4506);
});

test('currencyToAmount works with varied formats', () => {
setNumberFormat({ format: 'comma-dot', hideFraction: true });
expect(currencyToAmount('3,45')).toBe(3.45);
expect(currencyToAmount('3,456')).toBe(3456);
expect(currencyToAmount('3,45000')).toBe(345000);
expect(currencyToAmount("3'456.78")).toBe(3456.78);
expect(currencyToAmount("3'456.78000")).toBe(3456.78);
expect(currencyToAmount('1,00,000.99')).toBe(100000.99);
expect(currencyToAmount('1,00,000.99000')).toBe(100000.99);
});

test('currencyToAmount works with leading decimal characters', () => {
expect(currencyToAmount('.45')).toBe(0.45);
expect(currencyToAmount(',45')).toBe(0.45);
});

test('currencyToAmount works with negative numbers', () => {
expect(currencyToAmount('-3')).toBe(-3);
expect(currencyToAmount('-3.45')).toBe(-3.45);
expect(currencyToAmount('-3,45')).toBe(-3.45);
});

test('currencyToAmount works with non-fractional numbers', () => {
setNumberFormat({ format: 'comma-dot', hideFraction: false });
expect(currencyToAmount('3.')).toBe(3);
expect(currencyToAmount('3,')).toBe(3);
expect(currencyToAmount('3,000')).toBe(3000);
expect(currencyToAmount('3,000.')).toBe(3000);
});

test('currencyToAmount works with hidden fractions', () => {
setNumberFormat({ format: 'comma-dot', hideFraction: true });
expect(currencyToAmount('3.45')).toBe(3.45);
expect(currencyToAmount('3.456')).toBe(3.456);
expect(currencyToAmount('3.4500')).toBe(3.45);
expect(currencyToAmount('3.')).toBe(3);
expect(currencyToAmount('3,')).toBe(3);
expect(currencyToAmount('3,000')).toBe(3000);
expect(currencyToAmount('3,000.')).toBe(3000);
});

test('currencyToAmount works with dot-comma', () => {
setNumberFormat({ format: 'dot-comma', hideFraction: false });
expect(currencyToAmount('3,45')).toBe(3.45);
expect(currencyToAmount('3,456')).toBe(3.456);
expect(currencyToAmount('3,4500')).toBe(3.45);
expect(currencyToAmount('3,')).toBe(3);
expect(currencyToAmount('3.')).toBe(3);
expect(currencyToAmount('3.000')).toBe(3000);
expect(currencyToAmount('3.000,')).toBe(3000);
});
});
2 changes: 1 addition & 1 deletion packages/loot-core/src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export function currencyToAmount(currencyAmount: string): Amount | null {
if (
!match ||
(match[0] === getNumberFormat().thousandsSeparator &&
match.index + 4 === currencyAmount.length)
match.index + 4 <= currencyAmount.length)
) {
fraction = null;
integer = currencyAmount.replace(/[^\d-]/g, '');
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4503.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [jfdoming]
---

Fix number input on mobile with hidden decimals

0 comments on commit e10b105

Please sign in to comment.