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.
I 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.
This post walks through the conversion chain the way I implemented it — formulas, validation, and the precision pitfalls — with round demo numbers only.
Everything reduces to three values:
From the first two you get a concentration:
concentration (mcg/mL) = vialMassMg × 1000 / diluentMl
The ×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.
Demo numbers (chosen to be obviously illustrative): a 5 mg vial plus 2 mL of water gives 5000 / 2 = 2500 mcg/mL.
drawVolume (mL) = doseMcg / concentration (mcg/mL)
With 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.
Insulin syringes are graduated in "units", and for a U-100 syringe the definition is fixed: 100 units = 1 mL. So:
syringeUnits = drawVolumeMl × 100
0.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.
The 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:
const MCG_PER_MG = 1000;
const U100_UNITS_PER_ML = 100;
function concentrationMcgPerMl(vialMassMg, diluentMl) {
assertPositiveFinite(vialMassMg, 'vial mass');
assertPositiveFinite(diluentMl, 'diluent volume');
return (vialMassMg * MCG_PER_MG) / diluentMl;
}
function doseToDraw(doseMcg, concMcgPerMl) {
assertPositiveFinite(doseMcg, 'dose');
const ml = doseMcg / concMcgPerMl;
return { ml, units: ml * U100_UNITS_PER_ML };
}
There 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.
Most of the code is not the math — it is refusing bad math:
JavaScript floats make 0.1 + 0.2 !== 0.3, and dosage outputs are exactly where that stops being a joke. My approach:
One 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.
All 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.
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.