diff --git a/libs/common/src/lib/helper.spec.ts b/libs/common/src/lib/helper.spec.ts index e618dd698c..654b537cb2 100644 --- a/libs/common/src/lib/helper.spec.ts +++ b/libs/common/src/lib/helper.spec.ts @@ -29,6 +29,10 @@ describe('Helper', () => { ); }); + it('Get decimal number (with hyphenated text)', () => { + expect(extractNumberFromString({ value: 'BRK-B 425.30' })).toEqual(425.3); + }); + it('Get decimal number (comma notation)', () => { expect( extractNumberFromString({ locale: 'de-DE', value: '999,99' }) diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index 30b0c2a680..4f6739d5c7 100644 --- a/libs/common/src/lib/helper.ts +++ b/libs/common/src/lib/helper.ts @@ -210,13 +210,17 @@ export function extractNumberFromString({ value: string; }): number | undefined { try { - // Remove non-numeric characters (excluding international formatting - // characters and the minus sign to preserve negative values) - const numericValue = value.replace(/[^\d.,'’\s-]/g, ''); + // Only a leading minus sign indicates a negative value. Detect it before + // stripping so that hyphens within the text cannot flip the sign. + const isNegative = value.trim().startsWith('-'); + + // Remove non-numeric characters (excluding international formatting characters) + const numericValue = value.replace(/[^\d.,'’\s]/g, ''); const parser = new NumberParser(locale); + const parsedValue = parser.parse(numericValue); - return parser.parse(numericValue); + return isNegative ? -parsedValue : parsedValue; } catch { return undefined; }