A serving with 4.999 kcal can declare 0 Calories. At 5.000 kcal it declares 5. Sodium at 142.5 mg declares 140 mg. A general-purpose decimal formatter misses both rules because the raw amount determines the increment.
This implementation covers calories, total fat, and sodium for a U.S. Nutrition Facts panel. Keep the measured per-serving values in the recipe record and derive display values when rendering the label. The rules below come from paragraphs (c)(1), (c)(2), and (c)(4). 21 CFR 101.9(c) · FDA rounding table
Choose the increment from the original amount
| Field | Zero declaration | Lower increment | Higher increment |
|---|---|---|---|
| Calories | Below 5 kcal, where zero is permitted | 5 kcal through and including 50 kcal | 10 kcal above 50 kcal |
| Total fat | Below 0.5 g | 0.5 g below 5 g | 1 g above 5 g. At exactly 5 g both increments give 5 g. |
| Sodium | Below 5 mg | 5 mg from 5 through 140 mg | 10 mg above 140 mg |
Store incoming measurements as decimal strings. On import, fix the unit per field. If your supplier reports sodium in grams, convert to milligrams before applying its threshold. The code uses integer millionths of each unit. It accepts up to six fractional digits and raises an error if the input exceeds that precision. It rounds exact half increments upward for this implementation.
Save this block and the assertions below in label-rounding.ts and run node label-rounding.ts with Node.js 24.
const scale = 1_000_000n; type Nutrient = "calories" | "totalFat" | "sodium"; function parseAmount(input: string): bigint { const match = /^(\d+)(?:\.(\d{1,6}))?$/.exec(input.trim()); if (!match) throw new RangeError("Expected a nonnegative decimal with up to six fractional digits"); const [, whole, fraction = ""] = match; return BigInt(whole) * scale + BigInt(fraction.padEnd(6, "0")); } function roundTo(value: bigint, step: bigint): bigint { return ((2n * value + step) / (2n * step)) * step; } function displayAmount(value: bigint): string { const whole = value / scale; const fraction = (value % scale).toString().padStart(6, "0").replace(/0+$/, ""); return fraction ? `${whole}.${fraction}` : whole.toString(); } function declare(nutrient: Nutrient, input: string): string { const value = parseAmount(input); const rules = { calories: { zeroBelow: 5n * scale, boundary: 50n * scale, low: 5n * scale, high: 10n * scale }, totalFat: { zeroBelow: scale / 2n, boundary: 5n * scale, low: scale / 2n, high: scale }, sodium: { zeroBelow: 5n * scale, boundary: 140n * scale, low: 5n * scale, high: 10n * scale }, }[nutrient]; if (value < rules.zeroBelow) return "0"; return displayAmount(roundTo(value, value <= rules.boundary ? rules.low : rules.high)); } The integer division in roundTo truncates after adding half a step. Doubling the numerator and denominator keeps the operation exact. A value such as "0.499999" goes straight to the zero branch and cannot drift across the cutoff through a preliminary rounding step.
Test each branch and its neighbors
const cases: [Nutrient, string, string][] = [ ["calories", "4.999999", "0"], ["calories", "5", "5"], ["calories", "47.5", "50"], ["calories", "50", "50"], ["calories", "54.999999", "50"], ["calories", "55", "60"], ["totalFat", "0.499999", "0"], ["totalFat", "0.5", "0.5"], ["totalFat", "4.75", "5"], ["totalFat", "5", "5"], ["totalFat", "5.5", "6"], ["sodium", "4.999999", "0"], ["sodium", "5", "5"], ["sodium", "137.5", "140"], ["sodium", "140", "140"], ["sodium", "142.5", "140"], ["sodium", "145", "150"], ]; for (const [nutrient, amount, expected] of cases) { const actual = declare(nutrient, amount); if (actual !== expected) throw new Error(`${nutrient} ${amount}: got ${actual}, expected ${expected}`); } console.log(`${cases.length} boundary tests passed`); For a dual-column proof, two servings of 4.9 kcal each can yield 0 Calories per serving and 10 Calories per package. Apply the rules to the original 4.9 and 9.8 kcal separately. Pass the resulting strings to the label renderer, where the Calories row and the gram or milligram labels are presentation details. Serving sizes, Daily Values and nutrient-specific declarations require their own rules. 21 CFR 101.9(b)–(d)
For a visual check, enter the same measured amounts in the Nutrition Facts generator and compare its rounding audit with the exported proof. The maintainers of this article also publish the tool. Nutrition Facts generator
AI disclosure. This article was generated with AI.
