-2

I have been trying to get my output decimals to round to 2 places but am not using .toFixed or .round correctly.

Where is the correct place to put them?

$(function(){
    var val     = $('#height-slider').val();
    heightOutput  = $('#height-output');

    heightOutput.html(val);

    $('#height-slider').on('change', function(){
        heightOutput.html(this.value);
        $('#height-output1').html(parseInt(this.value)/2.42868520091);
        $('#height-output2').html(parseInt(this.value)/2.41585201564);
    });
});

JSFIDDLE

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Michael
  • 9,639
  • 3
  • 64
  • 69
  • If you have enough code to make a fiddle, you have enough code to include in your question. – Kevin B Feb 04 '14 at 20:03
  • This might be of some help http://stackoverflow.com/questions/5431440/jquery-round-decimal-to-49-or-99?rq=1 – ʞɹᴉʞ ǝʌɐp Feb 04 '14 at 20:04
  • possible duplicate of [In jQuery, what's the best way of formatting a number to 2 decimal places?](http://stackoverflow.com/questions/477892/in-jquery-whats-the-best-way-of-formatting-a-number-to-2-decimal-places) – Blazemonger Feb 04 '14 at 20:10

2 Answers2

2

.toFixed(2) is a method you call on the number. In this case, wrap parentheses around the division operation:

$('#height-output1').html((parseInt(this.value)/2.42868520091).toFixed(2));
$('#height-output2').html((parseInt(this.value)/2.41585201564).toFixed(2));

http://jsfiddle.net/mblase75/JQp9c/

.toFixed(2) is the appropriate method in this case, but you could use Math.round() this way instead:

$('#height-output1').html(Math.round(100*parseInt(this.value)/2.42868520091)/100);
$('#height-output2').html(Math.round(100*parseInt(this.value)/2.41585201564)/100);

http://jsfiddle.net/mblase75/fCLWU/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
2

Example: http://jsfiddle.net/KMPEX/1/

$(function(){
        var val     = $('#height-slider').val();
            heightOutput  = $('#height-output');

        heightOutput.html(val);

        $('#height-slider').on('change', function(){
            heightOutput.html(this.value);
            $('#height-output1').html((parseInt(this.value)/2.42868520091).toFixed(2));
            $('#height-output2').html((parseInt(this.value)/2.41585201564).toFixed(2));
        });
    });
Niels
  • 48,601
  • 4
  • 62
  • 81