{"record":{"id":"a5f9ec3f403555de","repo":"parallax/jsPDF","slug":"invalid-argument-passed-to-jspdf-roundtoprecision","errorCode":null,"errorMessage":"Invalid argument passed to jsPDF.roundToPrecision","messagePattern":"Invalid argument passed to jsPDF\\.roundToPrecision","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":494,"sourceCode":"  };\n\n  var advancedApiModeTrap = function(methodName) {\n    if (apiMode !== ApiMode.ADVANCED) {\n      throw new Error(\n        methodName +\n          \" is only available in 'advanced' API mode. \" +\n          \"You need to call advancedAPI() first.\"\n      );\n    }\n  };\n\n  var roundToPrecision = (API.roundToPrecision = API.__private__.roundToPrecision = function(\n    number,\n    parmPrecision\n  ) {\n    var tmpPrecision = precision || parmPrecision;\n    if (isNaN(number) || isNaN(tmpPrecision)) {\n      throw new Error(\"Invalid argument passed to jsPDF.roundToPrecision\");\n    }\n    return number.toFixed(tmpPrecision).replace(/0+$/, \"\");\n  });\n\n  // high precision float\n  var hpf;\n  if (typeof floatPrecision === \"number\") {\n    hpf = API.hpf = API.__private__.hpf = function(number) {\n      if (isNaN(number)) {\n        throw new Error(\"Invalid argument passed to jsPDF.hpf\");\n      }\n      return roundToPrecision(number, floatPrecision);\n    };\n  } else if (floatPrecision === \"smart\") {\n    hpf = API.hpf = API.__private__.hpf = function(number) {\n      if (isNaN(number)) {\n        throw new Error(\"Invalid argument passed to jsPDF.hpf\");\n      }","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L476-L512","documentation":"`roundToPrecision` (src/jspdf.js:488) is the low-level number formatter that backs `hpf`, `f2`, and `f3`. It throws at src/jspdf.js:493 when either the value or the resolved precision is NaN. Precision resolves to `precision || parmPrecision`, so it can be NaN if the constructor's `options.precision` was set to a non-numeric value. End users usually hit this indirectly — a NaN value (e.g. from a division by zero or a failed parseFloat) propagates into a drawing call that ultimately formats coordinates through roundToPrecision.","triggerScenarios":"Passing `options.precision: NaN` (or a non-numeric string coerced to NaN) to the jsPDF constructor; feeding NaN/undefined into any geometry method whose value is later formatted; a computed coordinate that became NaN through `0/0`, `Math.sqrt(-1)`, or `parseFloat(undefined)`.","commonSituations":"Layout code that computes positions from dynamic data where a divisor is zero; reading a missing numeric attribute from user data and passing `parseFloat(undefined)`; setting precision from a config value that is a string like `'high'` instead of a number.","solutions":["Trace the NaN to its source: log the value just before the failing API call and walk back to where it was computed.","Sanitize numeric inputs with a helper that coerces and defaults: `Number.isFinite(x) ? x : 0`.","If setting `options.precision`, pass a finite positive integer (e.g. 3).","For dynamically computed geometry, guard each step (division, sqrt, parseFloat) and fall back to a sane default."],"exampleFix":"// before\n doc.line(NaN, 0, x, y);   // NaN flows into roundToPrecision\n\n// after\n var safeX = Number.isFinite(x) ? x : 0;\n doc.line(safeX, 0, x, y);","handlingStrategy":"validation","validationCode":"function finiteNum(x, dflt) { return Number.isFinite(x) ? x : (dflt || 0); }\n// and ensure precision is a finite number when constructing:\nvar precision = Number.isFinite(opts.precision) ? opts.precision : undefined;\nnew jsPDF(Object.assign({}, opts, { precision: precision }));","typeGuard":"function isFiniteNumber(x) { return typeof x === 'number' && Number.isFinite(x); }","tryCatchPattern":"try {\n  doc.line(finiteNum(x), y, x2, y2);\n} catch (e) {\n  if (/roundToPrecision/.test(e.message)) {\n    console.warn('Skipping non-finite value in line()');\n  } else throw e;\n}","preventionTips":["Coerce every external number with Number.isFinite before passing to jsPDF.","Keep options.precision as a finite positive integer or undefined.","Add a boundary check in your render layer that rejects non-finite coordinates."],"tags":["numeric","precision","nan","formatting","internal"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}