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

A Number or Its toFixed() Text?

In this video, you rounded one pack total two ways and found out which result you can keep calculating with.

Key Points

  • Two ways to round: Math.round(totalKg * 100) / 100 rounds a total to two decimals and gives back a number. totalKg.toFixed(2) also rounds it to two decimals, keeping the zero at the end, and gives back a string.
  • Calling toFixed(): You write it after the number’s name with a dot in between, and put the number of decimals you want inside the parentheses.
  • Check the type: The quotes around '4.10' show that it’s text. typeof shownKg gives 'string', while typeof roundedKg gives 'number'.
  • + joins text: Adding 0.5 to the number 4.1 gives 4.6. Adding 0.5 to the string '4.10' gives '4.100.5', because when one side of + is a string, + joins the two values, the same way "5" + 2 gives '52'.
  • Keep the number, show the text: Keep your totals as numbers while you calculate, and call toFixed() only at the moment you show one.

In the REPL

Language: JavaScript
> const totalKg = 2.2 + 1.1 + 0.8
undefined
> totalKg
4.1000000000000005
> const roundedKg = Math.round(totalKg * 100) / 100
undefined
> roundedKg
4.1
> const shownKg = totalKg.toFixed(2)
undefined
> shownKg
'4.10'
> typeof shownKg
'string'
> typeof roundedKg
'number'
> roundedKg + 0.5
4.6
> shownKg + 0.5
'4.100.5'

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:00 In Round the Way Your Story Needs, you rounded numbers to a few decimals. Now say your day pack holds a tent, a sleeping bag and some food. In the Node REPL, you add their weights, 2.2, 1.1 and 0.8 kilograms.

00:16 The REPL shows the total with a tiny leftover at the end, like the one you met in When Numbers Surprise You. You want two decimals on your packing list. But later, you’ll add a water bottle to this total, so the rounded total has to work in a calculation, too.

00:32 In this video, you’ll round it two ways and find out which one you can keep calculating with.

00:39 The first way is the recipe you already know. Multiply by a hundred, round to a whole number, and divide by a hundred again. Store the result as roundedKg, and the REPL shows 4.1.

00:52 The leftover is gone.

00:56 The second way is new. The tool is toFixed(), and numbers in JavaScript carry it with them. Here, you write it after totalKg, with a dot in between, and put the number of decimals you want inside the parentheses.

01:11 Store this result as shownKg, and the REPL shows '4.10', rounded to two decimals, with the zero at the end kept. Look at the two results side by side in the values panel.

01:25 Your turn: pause the video and predict what typeof shownKg gives back.

01:35 It gives back string.

01:38 The quotes around '4.10' were the clue, the same quotes you saw on text in Turn Numeric Text Into Numbers. So here, toFixed() gave you a string.

01:48 For roundedKg, typeof answers number.

01:54 Now add the half-kilogram water bottle to roundedKg, and the REPL gives 4.6, the new total. And what if you added the same bottle to shownKg instead?

02:05 Your turn: pause the video and predict what the REPL shows for this line.

02:15 It shows '4.100.5', as text.

02:21 When one side of a plus sign is a string, plus joins the two values. You saw the same thing in Turn Numeric Text Into Numbers, where the text "5" plus the number 2 made '52'.

02:33 The bottle got glued onto the end of the text instead of added to the weight.

02:39 So the two ways give you different kinds of values. Rounding with Math.round() gives you a number, and you can keep calculating with it. toFixed() gives you a string, which is for showing the value, not for more math.

02:53 Keep your totals as numbers while you calculate, and call toFixed() only at the moment you show one. In the next lesson, Format Numbers Only When You Show Them, you’ll put that rule to work in your output lines.

Become a Member to join the conversation.