A topsoil calculator is one of the shortest tools you can build, and that is the point. The whole job is one multiplication, then a division. The mistakes people make with it are measurement mistakes, not maths mistakes, so the code should stay simple and the page should spend its words on measuring.
The formula is volume in cubic feet first, then cubic yards. Length in feet times width in feet times depth in feet gives cubic feet. Depth is usually measured in inches, so divide it by 12 first. A cubic yard holds 27 cubic feet, so divide the cubic feet by 27. Put together, cubic yards equals length times width times depth in inches, divided by 324.
function cubicYards(lengthFt, widthFt, depthIn) {
return (lengthFt * widthFt * depthIn) / 324;
}
Run a raised bed through it. Four feet wide, eight feet long, six inches deep. Four times eight times six is 192. Divide by 324 and you get about 0.59 cubic yards. Order one cubic yard and you have enough for the bed and a little left for topping up after the first rain. A bigger bed, ten feet by twelve feet at four inches deep, comes out at about 1.48 cubic yards. The numbers stay small enough to check by hand, which is worth doing the first time you test the tool.
Round beds need one extra step before the formula. Measure straight across the middle for the diameter, halve it for the radius, and work out the area as 3.14 times the radius squared. Then multiply by the depth in feet and divide by 27. In code that is:
function roundBedYards(diameterFt, depthIn) {
const radius = diameterFt / 2;
const area = 3.14 * radius * radius;
return (area * depthIn / 12) / 27;
}
For a bed with a curve or a corner cut out, split it into rectangles and run each part through the calculator on its own, then add the cubic yards together at the end. Adding the lengths together first gives the wrong answer when the depths differ, and they usually do. Depth is the number people measure least and get wrong most. Push a ruler into the bed in five or six places, take the average, and use that. If the readings run from 4 inches to 8 inches, the average is 6, and that is the number to use.
Two small touches make the tool feel finished. First, round the answer up to one decimal place and show the cubic feet beside it, so a user can see the working. Second, suggest ordering about a tenth more than the sum. Soil settles, beds are uneven, and a second small delivery costs more than the extra soil would have. That suggestion is not part of the maths, it is part of using the answer, and it belongs on the page.
The topsoil calculator runs the same formula with the measuring steps written out beside it, which is a good check for your own version. If your numbers match it on the 4 by 8 bed above, your code is right. If they do not, check the 324 first. It is the one number in the whole tool that is easy to mistype, and it is where most wrong answers come from.
Top comments (0)