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

Improve number handling for amount with leading decimal point. #3096

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/loot-core/src/shared/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe('utility functions', () => {
expect(looselyParseAmount('(3)')).toBe(-3);
});

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

test('looseParseAmount ignores non-numeric characters', () => {
// This is strange behavior because it does not work for just
// `3_45_23` (it needs a decimal amount). This function should be
Expand Down
7 changes: 6 additions & 1 deletion packages/loot-core/src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,15 @@ export function looselyParseAmount(amount: string) {
// Look for a decimal marker, then look for either 1-2 or 5-9 decimal places.
// This avoids matching against 3 places which may not actually be decimal
const m = amount.match(/[.,]([^.,]{5,9}|[^.,]{1,2})$/);
if (!m || m.index === undefined || m.index === 0) {
if (!m || m.index === undefined) {
return safeNumber(parseFloat(extractNumbers(amount)));
}

// for numbers starting with a decimal marker
if (m.index === 0) {
amount = '0' + amount;
}

const left = extractNumbers(amount.slice(0, m.index));
const right = extractNumbers(amount.slice(m.index + 1));

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3096.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [wdpk]
---

Improve number handling to handle leading decimal places.
Loading