Building a peptide dosage calculator: the unit-conversion math (mg/mcg, reconstitution, U-100 units)
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-purpo
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.
The three quantities
Everything reduces to three values:
- Vial mass — usually labelled in milligrams (mg).
- Diluent volume — the water added to reconstitute, in millilitres (mL).
- Target dose — usually prescribed in micrograms (mcg/µg).
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.
From dose to draw volume
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.
The U-100 syringe conversion
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 implementation
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.
Validation is the product
Most of the code is not the math — it is refusing bad math:
- Zero and negative inputs are rejected, not clamped. A diluent volume of 0 is a division by zero waiting to happen.
- Unit mismatch guards: if a user types a dose in mcg into a field expecting mg (or the reverse), the result is off by 1000×. I can't detect intent, but I can sanity-check ranges: a computed draw volume larger than the whole vial, or a dose larger than the vial's total mass, triggers an explicit warning instead of a confident number.
- Impossible precision: if the draw volume works out below what the smallest syringe graduation can show, the tool says so rather than printing six decimal places.
Rounding: the subtle part
JavaScript floats make 0.1 + 0.2 !== 0.3, and dosage outputs are exactly where that stops being a joke. My approach:
- Compute at full precision internally; round only at display time.
- Display mL to 2 decimal places and units to the nearest 0.5 unit, because that is what syringe markings support.
- Never round an intermediate value and feed it back into the next step — the classic bug is rounding the concentration first, then computing the draw from the rounded concentration and compounding the error.
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.
Running it
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.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.