Rounding for Display With toFixed()

The batch planner now accepts tonight’s order as a number. What’s left are the four output lines the head roaster reads. JavaScript’s template literals have no format specs, so every :.1f and :.2f in batch_plan.py needs a new home, and where you put it matters.

Turn a Format Spec Into .toFixed()

Every JavaScript number has a .toFixed() method that takes the number of decimal places, so Python’s {green_kg:.1f} becomes greenKg.toFixed(1) in a ${} placeholder. This port also asks typeof what comes back:

Language: JavaScript
const orderKg = 37.5;
const WEIGHT_LOSS = 0.15;

const greenKg = orderKg / (1 - WEIGHT_LOSS);

console.log(`Green beans: ${greenKg.toFixed(1)} kg`);
console.log(typeof greenKg.toFixed(1), typeof greenKg);

The first line matches Python’s, and the second shows the two types:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.