Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Rounding With Math.round()

Rounding With Math.round()

In this lesson, you ported the batch planner’s bags and cost lines, and found where JavaScript’s rounding parts ways with Python’s.

  • JavaScript has no math module to import. Its math functions live on Math, with a capital M, a built-in global object that’s always there. Watch out: JavaScript’s form of the import, import math from "math", stops the file with ERR_MODULE_NOT_FOUND before any line runs.
  • math.ceil() becomes Math.ceil(), which rounds up as Python’s does, so the 37.5 kg order needs 150 bags, though it gives back a plain number, not an int. Math also holds Math.sqrt() and Math.PI (Python’s math.pi), and Math.random() for random numbers.
  • Python’s built-ins round(), abs(), max() and min() move onto Math too. Watch out: abs() or round() on its own is a ReferenceError.
  • Math.abs(-75) is 75, and Math.max(12, 8, 17.5) and Math.min(12, 8, 17.5) are 17.5 and 8. They take separate values; finding the largest in an array comes later.
  • Math.round() always rounds to a whole number: unlike Python’s round(), it has no place for a number of decimals. Where Python finds no place for an argument, it raises a TypeError, but JavaScript doesn’t check how many arguments you pass, and Math.round() reads only the first. So Math.round(greenKg * GREEN_PRICE_PER_KG, 2) gives 326, and the planner prints Green coffee: €326 where Python prints Green coffee: €326.47.
  • To round to cents, scale up, round, and scale back: Math.round(rawCost * 100) / 100. Times 100, the cost is 32647.058823529413 cents, Math.round() makes that 32647, and dividing by 100 gives 326.47.
  • That rounds for further calculation. Showing a price with exactly two decimals is a separate job, for a later lesson.
  • Watch out for exact halves: Math.round() rounds a half up, toward positive infinity, so Math.round(2.5) is 3 where Python’s round(2.5) is 2. Below zero, Math.round(-2.5) is -2; Python’s round(-2.5) is -2 too, but only because -2 is even. On the cents line, an exact half-cent splits the same way: 0.125 rounds to 0.13 in JavaScript and 0.12 in Python.

Here are just the two new lines and the settings they use; the batch lines stay where they are:

Language: JavaScript
const WEIGHT_LOSS = 0.15;
const BAG_G = 250;
const GREEN_PRICE_PER_KG = 7.4;

const orderKg = 37.5;
const greenKg = orderKg / (1 - WEIGHT_LOSS);
const bags = Math.ceil((orderKg * 1000) / BAG_G);
const rawCost = greenKg * GREEN_PRICE_PER_KG;
const cost = Math.round(rawCost * 100) / 100;
console.log(`Bags: ${bags}`);
console.log(`Green coffee: €${cost}`);

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

00:02 In this lesson, you’ll port the batch planner’s next two lines, the bags and the cost, below the batch lines you ported earlier, and see where JavaScript’s rounding parts ways with Python’s.

00:14 math.ceil() rounds a part-filled bag up to a whole one, though tonight’s order fills exactly a hundred and fifty bags. round() keeps the cost to two decimals, so the script prints it to the cent.

00:29 JavaScript has no math module to import. Its math functions, Math.floor() included, live on Math, capital M, a built-in global object that’s always there, so the import line goes.

00:43 Watch out: write JavaScript’s version of that import, and the file stops before any line runs. The settings and the green-bean line carry over, and math.ceil() becomes Math.ceil(), which rounds up as Python’s does, but the result isn’t an int, only a number.

01:02 Math also holds Math.sqrt(), Math.PI, and Math.random(), for random numbers.

01:11 Python’s round(), abs(), max() and min() are built-ins, outside the math module. In JavaScript, they move onto Math too.

01:20 Watch out: a bare abs() or round() stops the file with a reference error. Math.abs() turns the early roast’s minus seventy-five seconds into seventy-five.

01:32 Math.max() and Math.min() pick the evening’s largest and smallest orders. They take separate values; arrays come later.

01:45 The cost line then ports as Math.round(), with the 2 kept as its second argument. The print lines become console.log() calls.

01:54 Python printed the cost to the cent. What will JavaScript print for the green coffee? Your turn: pause the video and predict.

02:08 Three hundred and twenty-six euros, and not a cent more. There’s no error and no warning. The cents have vanished.

02:20 Math.round() always rounds to a whole number. Python’s round() has a place for the 2, and Math.round() has none.

02:29 Where Python finds no place for an argument, as with math.ceil(), it stops with a type error. JavaScript doesn’t check how many arguments you pass, so there’s no error: Math.round() reads the first one and drops the 2.

02:46 To round to cents, the fix splits the cost line in two: the raw cost, then the rounding. Times a hundred, the cost counts cents. Math.round() rounds that to whole cents.

03:01 Divided by a hundred, it’s back in euros. Run it again, and for this order it prints Python’s cost to the cent.

03:10 This rounds for further calculation. Showing a price with two decimals is a later lesson’s job.

03:19 Watch out for exact halves. Python’s round() sends a half to the even neighbor: round(2.5) gives 2, rounding down. Math.round() always rounds a half up, toward positive infinity: Math.round(2.5) gives 3 instead.

03:38 Below zero, up means toward zero, so Math.round(-2.5) is -2; Python agrees only because -2 is even. The cents line splits the same way: exactly twelve and a half cents rounds to thirteen in JavaScript and twelve in Python.

04:00 Where Python reaches for the math module or a built-in, reach for Math, with no import. To round to the cent, scale by a hundred, round, and divide back.

04:12 Next up: the exercise Count the Bags and Cost the Beans. For a small order of eighteen point one kilos, you’ll work out the bags to label, the green beans to weigh out, and the cost to the cent with delivery, to match the Python script.

Become a Member to join the conversation.