Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing decimals into a span

<input value="123" />
<span></span>

$('input').change(function (){
    var val = $(this).val().toFixed(2);
    $('span').text(val);
});

I have tried the code above but can't seem to get the decimal on the text() is just shows as 123. This is my first time experimentind with toFixed()

just in case toFixed() is a js native method not a jquery

like image 214
Val Avatar asked Dec 17 '22 16:12

Val


1 Answers

toFixed is a method that you would call on a number, not on a string. You need to parse the string to a number:

var val = parseFloat($(this).val(), 10).toFixed(2);

Live demo here: http://jsfiddle.net/QrW5C/

like image 124
Darin Dimitrov Avatar answered Dec 19 '22 06:12

Darin Dimitrov