-1
var money = prompt("Enter an amount of money");
money = parseFloat(money);
var months = prompt("Enter how long you will be investing for in months");
months = parseInt(months);
months = Math.round(months);
var interest = prompt("Enter an interest rate you would like to test your investment at");
interest = parseFloat(interest);
console.log(months);

Why does this code round down the months no matter what? What do i have to do differently?

Justin
  • 1,329
  • 3
  • 16
  • 25
  • 3
    Because you truncate the months when you say `parseInt(months)`? – cHao Jan 10 '14 at 17:34
  • Duplicate, there's lots of answers about this: http://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript/7343013#7343013 – jcollum Jan 10 '14 at 17:35
  • RTFM - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt. "parseInt truncates numbers to integer values." – j08691 Jan 10 '14 at 17:35
  • @j08691 don't say "RTFM", it's rude – jcollum Jan 10 '14 at 17:36
  • @jcollum Sounds like he might have read 1 to many questions of this nature for the day. – crush Jan 10 '14 at 17:36
  • @jcollum: This isn't a duplicate of the question you linked. This isn't actually about rounding to anything but whole numbers. – cHao Jan 10 '14 at 17:36
  • 2
    @jcollum - doesn't the "f" mean "fantastic"? – j08691 Jan 10 '14 at 17:37
  • Yeah not a dupe of that question, but there's soooo many rounding questions on here that it must be a dupe of one of them – jcollum Jan 10 '14 at 17:37
  • @j08691 Hah, you know it doesn't. I think what this guy really needs is to learn how to debug his js. Stepping through the code and adding some watches would've solved this quickly. – jcollum Jan 10 '14 at 17:39
  • What's odd is that he *used* `parseFloat` in other locations - apparently without knowing the difference. – crush Jan 10 '14 at 17:40
  • @j08691 Im new to javascript so i dont really know how to debug, i kind of understand the consoles in web browsers, but what are the "watches" i could've added? – Justin Jan 13 '14 at 02:38

2 Answers2

2

The function parseInt converts the number to an integer, which doesn't support fractional numbers (decimals).

months = parseInt(months);
months = Math.round(months);

Use parseFloat here instead:

months = parseFloat(months);
months = Math.round(months);
crush
  • 16,713
  • 9
  • 59
  • 100
0

Why would you need to round the int in the first place? It should always be a whole number, right?

That said, never use parseInt without also specifying the radix, which in your case should be 10.

months = parseInt(months, 10);
Sean Kinsey
  • 37,689
  • 7
  • 52
  • 71
  • I was a bit thrown by that at first as well, but it seems that he must be allowing for a fractional months to be input. Only way this question makes sense. Strange. – crush Jan 10 '14 at 17:35
  • `Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.` – crush Jan 10 '14 at 17:38