$('input').change(function (){ var val = $(this).val().toFixed(2); $('span').t...">

Parsing decimal places in between

<input value="123" /> <span></span> $('input').change(function (){ var val = $(this).val().toFixed(2); $('span').text(val); }); 

I tried the code above, but I can’t get the decimal value on text() , it just displays as 123. This is my first experiment with the toFixed() tag

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

+4
source share
3 answers

toFixed is a method that you call by number, not by line. You need to parse the string into a number:

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

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

+12
source

Look at the error console. You should see something like:

 TypeError: Object 123 has no method 'toFixed' 

Lines do not have a toFixed() method. Only numbers. First convert the value to a number (e.g. by adding + ):

 var val = (+$(this).val()).toFixed(2); 

Demo

0
source

You need to parse it:

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

info here: http://www.bennadel.com/blog/1013-Javascript-Number-toFixed-Method.htm

and

 <span></spam> (?) 
0
source

Source: https://habr.com/ru/post/1342291/


All Articles