{"slug": "building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units", "title": "Building a peptide dosage calculator: the unit-conversion math (mg/mcg, reconstitution, U-100 units)", "summary": "A developer built a client-side peptide dosage calculator that converts vial mass in milligrams and diluent volume into concentration, draw volume, and U-100 syringe units, with the conversion chain normalized to micrograms and milliliters at the input boundary to prevent 1000x unit errors. The implementation isolates the math in pure functions backed by a test table, validates inputs, and displays every intermediate step so unit mistakes are visible. The developer notes that rounding and floating-point precision, not the UI, consumed most of the build effort.", "body_md": "*Educational purposes only. This post is about arithmetic and software, not medical advice. Nothing here tells anyone how much of anything to take — always follow your clinician's instructions.*\n\nI build small single-purpose web tools, and the peptide dosage calculator was supposed to be the easy one. It is \"just\" unit conversion: a dose in micrograms, a vial in milligrams, some water, a syringe graduated in units. Three inputs, one division. It took longer to get the math *trustworthy* than to build the UI, and the reasons are the usual ones: unit traps, floating point, and rounding decisions that actually matter when the output is a volume someone draws into a syringe.\n\nThis post walks through the conversion chain the way I implemented it — formulas, validation, and the precision pitfalls — with round demo numbers only.\n\nEverything reduces to three values:\n\nFrom the first two you get a **concentration**:\n\n```\nconcentration (mcg/mL) = vialMassMg × 1000 / diluentMl\n```\n\nThe ×1000 is where the first bug class lives: 1 mg = 1000 mcg, and mixing the two scales is a 1000× error, not a rounding error. My rule in code: convert everything to mcg and mL at the boundary, immediately, and never let mg exist inside the calculation core.\n\nDemo numbers (chosen to be obviously illustrative): a 5 mg vial plus 2 mL of water gives 5000 / 2 = **2500 mcg/mL**.\n\n```\ndrawVolume (mL) = doseMcg / concentration (mcg/mL)\n```\n\nWith the demo concentration, a hypothetical 250 mcg dose is 250 / 2500 = **0.10 mL**. Clean numbers are a lie, though — real inputs produce things like 0.0837 mL, which no syringe can measure. That is a display and rounding problem, covered below.\n\nInsulin syringes are graduated in \"units\", and for a U-100 syringe the definition is fixed: **100 units = 1 mL**. So:\n\n```\nsyringeUnits = drawVolumeMl × 100\n```\n\n0.10 mL → 10 units. This is the number people actually read off the barrel, so it is the primary output of the tool, with mL shown alongside. The trap: this only holds for U-100 syringes. A U-40 syringe would be ×40. The calculator states the U-100 assumption next to the result instead of burying it in a help page.\n\nThe core is a handful of pure functions. Keeping them pure made the unit tests trivial, which matters because the test table *is* the specification here:\n\n``` js\nconst MCG_PER_MG = 1000;\nconst U100_UNITS_PER_ML = 100;\n\nfunction concentrationMcgPerMl(vialMassMg, diluentMl) {\n  assertPositiveFinite(vialMassMg, 'vial mass');\n  assertPositiveFinite(diluentMl, 'diluent volume');\n  return (vialMassMg * MCG_PER_MG) / diluentMl;\n}\n\nfunction doseToDraw(doseMcg, concMcgPerMl) {\n  assertPositiveFinite(doseMcg, 'dose');\n  const ml = doseMcg / concMcgPerMl;\n  return { ml, units: ml * U100_UNITS_PER_ML };\n}\n```\n\nThere is also a second input mode: some labels state the concentration directly (mcg/mL or mg/mL), so the tool accepts a known concentration and skips the reconstitution step. Both modes converge on the same `doseToDraw`, which keeps the tested surface small.\n\nMost of the code is not the math — it is refusing bad math:\n\nJavaScript floats make `0.1 + 0.2 !== 0.3`, and dosage outputs are exactly where that stops being a joke. My approach:\n\nOne more UI decision: show the full chain (\"2500 mcg/mL → 0.10 mL → 10 units\") instead of just the final number. When every intermediate step is visible, a unit mistake is obvious at a glance — the user can catch what validation can't.\n\nAll of this runs client-side in the browser; nothing is stored unless the user explicitly saves a calculation. If you want to try the tool itself, it is here: [Peptide Dosage Calculator](https://peptidedosagecalculator.app).\n\n*Educational purposes only — this post covers software arithmetic, not medical advice, and it does not recommend any dose of any substance. Follow your clinician's instructions.*", "url": "https://wpnews.pro/news/building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units", "canonical_source": "https://dev.to/gabriellaandrews/building-a-peptide-dosage-calculator-the-unit-conversion-math-mgmcg-reconstitution-u-100-units-3mkn", "published_at": "2026-11-25 06:32:20+00:00", "updated_at": "2026-11-25 06:42:26.707399+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Peptide Dosage Calculator"], "also_reported_by": [], "takeaway": "Normalize all inputs to micrograms and milliliters at the boundary and keep the calculation core in pure functions, since mixing mg and mcg produces a 1000x error rather than a rounding error.", "recommendation": {"advice": "Convert units at the input boundary and display the full conversion chain (concentration, mL, syringe units) so users can spot unit errors that validation alone cannot catch.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units", "markdown": "https://wpnews.pro/news/building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units.md", "text": "https://wpnews.pro/news/building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units.txt", "jsonld": "https://wpnews.pro/news/building-a-peptide-dosage-calculator-the-unit-conversion-math-mg-mcg-u-100-units.jsonld"}}