From 74b907aa154ab0ce3fecd17e8f5d1a3bed133241 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:42:45 +0530 Subject: [PATCH] fix(import): read 1,150 as 1150, not 1.15 parseNumber turned the comma of any single-comma number into a decimal point, although its comment says 1,500 is a thousands separator. A spreadsheet export with a thousands format writes the displayed value, so a 1,150 kcal dinner was saved as 1.15 kcal. A comma before groups of three digits now separates thousands; 1,5 is still a decimal comma. --- scripts/nutrition-import-numbers.test.js | 18 ++++++++++++++++++ server/lib/nutrition-import/common.js | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 scripts/nutrition-import-numbers.test.js diff --git a/scripts/nutrition-import-numbers.test.js b/scripts/nutrition-import-numbers.test.js new file mode 100644 index 00000000..e73b3795 --- /dev/null +++ b/scripts/nutrition-import-numbers.test.js @@ -0,0 +1,18 @@ +/** + * Spreadsheet exports keep the displayed value, so a column with a thousands + * format writes "1,150". That is 1150, not 1.15; "1,5" stays a decimal comma. + */ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { parseNumber } from '../server/lib/nutrition-import/common.js'; + +test('a comma before groups of three digits separates thousands', () => { + assert.equal(parseNumber('1,150'), 1150); + assert.equal(parseNumber('12,000'), 12000); + assert.equal(parseNumber('1,250.5'), 1250.5); +}); + +test('a lone comma with one or two decimals is a decimal comma', () => { + assert.equal(parseNumber('1,5'), 1.5); + assert.equal(parseNumber('12,25'), 12.25); +}); diff --git a/server/lib/nutrition-import/common.js b/server/lib/nutrition-import/common.js index bbcbe784..2d1eed5b 100644 --- a/server/lib/nutrition-import/common.js +++ b/server/lib/nutrition-import/common.js @@ -257,7 +257,10 @@ export function parseNumber(s) { // Handle "1,5" but NOT "1,500" (thousands separator). If there's exactly one // comma and no period, treat comma as decimal. let normalized = str; - if (normalized.includes(',') && !normalized.includes('.')) { + if (/^[+-]?\d{1,3}(,\d{3})+(\.\d+)?$/.test(normalized)) { + // "1,150", "12,000", "1,250.5": commas group thousands + normalized = normalized.replace(/,/g, ''); + } else if (normalized.includes(',') && !normalized.includes('.')) { const commas = normalized.split(',').length - 1; if (commas === 1 && /^\d+,\d+/.test(normalized)) { normalized = normalized.replace(',', '.');