1

This should be a very easy and quick one, but I don't understand why this is not working.

I'm trying to convert a string ("50.0") to a number using parseFloat()

This is the code:

 // Lets say e.value = 20
 var valueStr = e.value + '.0';   // valueStr = "20.0";           
 value = parseFloat(valueStr);
 console.log (value);

I am expecting value to be 20.0, but it returns 20.

I've checked this example and it does exactly the same thing, but it works in the example.

Here's a codepen with the full code (i'm modifying a jquery plugin called "roundSlider")

http://codepen.io/anon/pen/mPyxyv

Thanks in advance for any help.

Nick
  • 13,493
  • 8
  • 51
  • 98
  • 6
    You are looking for [Number.prototype.toFixed()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed). `parseFloat(valueStr).toFixed(1);` you can also look at this question and the corresponding answers: [Format number to always show 2 decimal places](http://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – t.niese Mar 01 '16 at 11:24
  • that worked, on the console. Not as the plugin value but i'm guessing there's something else blocking it to be displayed as .0 value. Thanks for the help – Nick Mar 01 '16 at 11:25
  • check this link http://stackoverflow.com/questions/4868556/how-do-i-stop-parsefloat-from-stripping-zeroes-to-right-of-decimal – shu Mar 01 '16 at 11:27
  • But whats the Problem. You want to display it, then as a string? – Kordi Mar 01 '16 at 11:36
  • The first comment solved this question, it was returning as a number without .0 at the end – Nick Mar 01 '16 at 11:37

0 Answers0