Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjohnathanmyers
19,994 PointsSo I noticed in the following test that when I created a variable that contains a float the number 360.00 is not a float
So I noticed in the following test that when I created a variable that contains a float, the number 360.00 is not a float. So is it safe to assume that just because a "." is used that does not mean that the number is a float if the following numbers to the right of the number equal zero? I can see where this might get tricky if you where programming with money and the program was expecting a float but the amount was an even no change amount. I've been over some other Javascript courses but I never noticed this.
2 Answers
William Li
Courses Plus Student 26,868 PointsIt's a float, In fact, every number in JavaScript is just a 64 bit floating point.
Marcus Parsons
15,719 PointsHey Johnathan,
You can force decimals to display with the normal "toFixed(n)" method where n is the number of decimal places you wish to display, and this works especially well with money since you only want to deal with 2 decimal places. So, let's say there is a transaction between two variables and you want to display in dollars, you can just do like so:
var amt1 = 400;
var amt2 = 40;
var total = "$" + (amt1 - amt2).toFixed(2);
//Displays $360.00
console.log(total);
If you're doing any other currency besides dollars, you can use a library such as the accounting.js library.
johnathanmyers
19,994 PointsThanks Marcus,
Sometimes I think too hard on simple things and my brain breaks. :) I'm adding accounting.js to my bookmarks.
Marcus Parsons
15,719 PointsI know exactly what you mean haha I call those "brain burps" haha It's a great library for dealing with currency!
johnathanmyers
19,994 Pointsjohnathanmyers
19,994 PointsThanks William