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 trial

JavaScript JavaScript Foundations Numbers Parsing Numbers From Strings

JavaScript doesn't seem fit to parse the "decimals" of octimal numbers, i.e. the numbers after the point. Thoughts?

I would like to hear if there are ways around this problem, or whether it is in fact a limitation of the JS language.

To specify what is going on. Instead of parsing the string as an octimal number, the following code:

console.log(parseFloat("10.135", 8));

just returns 10.135. If I enter the following code:

console.log(010.135);

the return reads: "Uncaught SyntaxError: Unexpected number"

So, am I correct in concluding that JS cannot work with non-ten-based numbers in "decimals".

2 Answers

La Kenzi
La Kenzi
14,346 Points

The parseFloat() function parses a string and returns a floating point number.

This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Note: Only the first number in the string is returned!

Note: Leading and trailing spaces are allowed.

Note: If the first character cannot be converted to a number, parseFloat() returns NaN or in other words this error "Uncaught SyntaxError: Unexpected number".

console.log(010.135);

Uncaught SyntaxError: Unexpected number

console.log(parseFloat("010.135"));

10.135

Hopefully this gives a better understanding of how JavaScript interprets numbers and decimal numbers. http://www.w3schools.com/jsref/jsref_parsefloat.asp

La Kenzi
La Kenzi
14,346 Points

Yes, when using console.log(010.135); JavaScript can't work with non-ten-based numbers, it can only work if you use syntax from the above post.