A grams to mL calculator should make density a required independent input. It must retain that assumption and its units beside the result, rather than defaulting to water or guessing a material from the number.
This original two-mode helper finds volume from mass and density, or mass from volume and density. Its examples are fictional mathematical records, not ingredient, material, sampling or dosing instructions. The local HTML example provides the two calculation modes; verify its behavior in the intended host before relying on a result.

Make the missing dependency visible
Harvard Project Zero's density module describes density as mass divided by volume and rearranges that relationship to find either mass or volume when density is known. It also uses mass-per-volume units such as g/cm³ and g/mL. This design adopts the relationship, not its material examples, lesson art or learning-outcome claims. Harvard Project Zero: Causal Patterns in Density
Let ρ represent the supplied density. With mass m in grams and volume V in milliliters:
V = m ÷ ρ, with ρ in g/mL.
m = ρ × V.
Dividing grams by grams per milliliter leaves milliliters. Merely changing the label from g to mL does not perform that calculation. If density is missing, the result is unavailable; the tool cannot infer it from mass alone.
These formulas concern the mass and volume represented by the supplied density relationship. Do not substitute a component's mass in a final mixture for the whole sample's mass and then call its concentration the sample density.
Normalize the supported units first
Keep mass choices g and kg, volume choices mL and L, and density choices g/mL, g/cm³ and kg/L. Use distinct IDs and full labels so the denominator is not lost during selection.
| Supplied unit | Normalization |
|---|---|
| kg mass | Multiply by 1,000 to obtain g |
| L volume | Multiply by 1,000 to obtain mL |
| g/cm³ density | Same numeric value as g/mL because 1 cm³=1 mL |
| kg/L density | Same numeric value as g/mL: both numerator and denominator scale by 1,000 |
For example, 0.8 kg/L becomes 0.8 g/mL, not 800 g/mL. Show the numerator and denominator cancellation rather than applying only the mass conversion.
The numeric equivalence applies to these named density units. It is not permission to accept g/L as though it also has the same value. Unsupported density units are rejected, with an explanation of the whitelist.
Normalize inputs into g, mL and g/mL, calculate once, then convert to the selected output unit. A liter output is the calculated mL divided by 1,000; a kilogram output is the calculated g divided by 1,000.
Compare assumptions without naming a material
Use independent fictional records:
| Record | Supplied quantity | Supplied density | Result |
|---|---|---|---|
| D01 | 10 g | 0.8 g/mL | 12.5 mL |
| D02 | 10 g | 2 g/mL | 5 mL |
| D03 | 12.5 mL | 0.8 g/mL | 10 g |
| D04 | 0.01 kg | 0.8 kg/L | 12.5 mL |
D01 and D02 differ because their density assumptions differ. The smaller result does not mean the tool lost mass. D03 reverses D01's relationship. D04 represents the same mass/density values in other supported units.
For D04, show 0.01 × 1,000 = 10 g, 0.8 kg/L = 0.8 g/mL, then 10 ÷ 0.8 = 12.5 mL. If the selected output is L, the result is 0.0125 L.
No row identifies a substance, temperature or measurement method. The densities are fictional inputs for inspecting the formulas. The helper neither looks them up nor certifies them.
Preserve an assumption record
Provide a readable text record containing mode, raw quantity and unit, raw density and unit, normalized values, selected output unit, equation, internal result and displayed result. Label density as Supplied assumption rather than Verified density.
For D01, that record reads: volume mode; mass 10 g; supplied density 0.8 g/mL; V=10÷0.8; calculated volume 12.5 mL. Someone reviewing the answer can then see exactly which density made it possible.
Keep the record selectable for ordinary copying. Do not advertise file export, automatic storage or a copied record as evidence that the assumption was checked independently.
Compute with unrounded internal values and format visible results to at most six decimals where needed. Mark rounded display values as approximate. A small positive result must not be described as exact zero because of formatting.
Validate density separately from the numerator
For this bounded design, quantity fields accept finite nonnegative dot-decimal values up to 1,000,000 with at most six fractional digits. Density accepts the same grammar and maximum but must be strictly positive.
Zero mass with positive density gives zero volume. Zero volume with positive density gives zero mass. Zero density is invalid in both modes; it cannot be used as a denominator or treated as a missing material lookup.
Reject blank quantity/density, negatives, commas, exponent notation, nonfinite values, missing unit selections and unsupported IDs. Trim surrounding whitespace for validation while retaining raw input in the record. These parser/range limits are local interface choices, not scientific measurement limits.
Use native mode and unit selectors, labeled quantity/density fields, Calculate and Reset. A mode change clears previous fields/results and changes labels deliberately. Editing any input invalidates the old result and assumption record. Invalid submission shows feedback without retaining a plausible answer from earlier input.
Reset empties fields, mode/unit selections, record and feedback. Keyboard, narrow layout and hosted behavior need actual tests before the helper is presented as working.
Copy this prompt to specify the native helper
Create an original self-contained HTML/CSS/JavaScript density-dependent mass/volume helper, complete native source, selectable text assumption record and manual test matrix. Cite https://pz.harvard.edu/sites/default/files/CausalPatternsInDensity.pdf for ρ=m/V, V=m/ρ and m=ρV concepts only. Do not copy lesson art/material examples or claim learning outcomes. No network/storage, material/food lookup, water default, recipe, sampling, dosing or certified density.
Two modes: supplied mass plus density→volume; supplied volume plus density→mass. Mass units g/kg; volume mL/L; density g/mL, g/cm³ or kg/L. Normalize to g, mL and g/mL. kg mass×1000; L volume×1000. g/cm³ and kg/L retain the same numeric density value as g/mL; show numerator/denominator cancellation. Unsupported g/L or other units must not be treated as equivalent. Convert final g or mL to the selected whitelisted output unit only after computing.
Independent fictional fixtures: D01 10 g at 0.8 g/mL→12.5 mL; D02 10 g at 2 g/mL→5 mL; D03 12.5 mL at 0.8→10 g; D04 0.01 kg at 0.8 kg/L→12.5 mL or 0.0125 L. Densities are supplied mathematical assumptions, not identified materials or measurements. Never infer density from quantity alone or confuse component concentration with whole-sample density.
Manual grammar: digits with optional dot, at most six fractional digits, finite magnitude ≤1000000; trim outer whitespace but retain raw text. Quantity is nonnegative, density strictly positive. Zero quantity with positive density gives zero result. Blank or zero density, negative, comma/exponent/nonfinite input, missing/unsupported units reject and clear current output. Limits are interface choices.
Native mode/unit selectors, quantity/density fields, Calculate and Reset. Mode change clears old fields/results and changes labels. Any edit invalidates previous output/record. Reset empties all fields/selections/feedback/record. Output includes original units, normalized values, density labeled Supplied assumption, equation, internal and displayed result. Text record supports ordinary selection/copy; no export or storage promise. Use unrounded internal calculations and at most six displayed decimals with approximation context; do not label tiny positives exact zero.
Test all four records, output-unit scaling, all three density-unit equivalences, zero quantity versus zero/missing density, wrong units, grammar/range, mode/stale-result behavior and Reset. Check keyboard, actual article width and 360 px without page overflow. Separate performed arithmetic from proposed runtime tests; no built calculator, hosted execution or accessibility claim before testing. Native source and assumption record remain authoritative; optional NOS explanation promises no saved calculations, export or automatic updates.
Keep D01's density beside the result
Maintain the native file and original assumption records together. The contrast between D01 and D02 belongs in the explanation because it shows why the density field is required rather than a cosmetic option.
NOS can save and render HTML. A prepared HTML explanation can present those fictional records and their unit cancellations beside the maintained source. Actual hosted mode changes, Calculate, Reset and text selection need verification before claiming a working helper.
Explore NOS with the density record. The reader should retain both the converted quantity and the supplied assumption that connects its mass to its volume.