1

I'm trying to set the number of decimal places on my stock widget, so that 0.199999 would just appear as 0.19, and 1 would appear as 1.00, and 8.6543 would just appear as 8.65. What's wrong with the script as I have it?

Here is what I've got currently...

var rndchange = obj.ChangeRealtime;

$tr.append($('<td class="'+changeClass+'">').text(rndchange.toFixed(2)|| "--"));

This is the entire script...

$(function () {
    $.getJSON('http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.quotes%20WHERE%20symbol%20in(%22GCF14.CMX%22%2C%22SIF14.CMX%22%2C%22PAH14.NYM%22%2C%22PLF14.NYM%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=').done(function (data) {
    console.log("data: ", data);
    console.log(data.query.results.quote);
    $.each(data.query.results.quote, function (key, obj) {
        var changeClass = '';
        var changeInPercentClass = '';
        var rndchange = obj.ChangeRealtime;
        var $tr = $('<tr/>', {
            'class': 'my-new-list'
        }).appendTo('#blk-1 table');
        $tr.append($('<td id="name" class="cells"/>').text(obj.Name.split(' ')[0] || "--"));
        $tr.append($('<td class="cells"/>').text(obj.AskRealtime || "--"));
        //$tr.append($('<td/>').text(obj.BidRealtime || "--"));

        (obj.Change.substr(0,1) === '+') ? changeClass = 'green' : changeClass = 'red';

        (obj.Change.substr(0,1) === '+') ? changeInPercentClass = 'green' : changeInPercentClass = 'red';

        $tr.append($('<td class="'+changeClass+'">').text(rndchange.toFixed(2)|| "--"));
        var re = /([+|-]\d\.\d\d\%)/gi;
        var rt = re.exec(obj.ChangePercentRealtime);
        $tr.append($('<td class="'+changeInPercentClass+'">').text( rt[0] || "--"));
      });
   });
});

Here is my fiddle: http://jsfiddle.net/thetuneupguy/JaCaH/7/

  • possible duplicate of [Display two decimal places, no rounding](http://stackoverflow.com/questions/4187146/display-two-decimal-places-no-rounding) – Andrew Whitaker Jan 13 '14 at 22:46

2 Answers2

2

You can used JavaScript's built in toFixed() method to change a value to a fixed number of decimal places:

var n = 1;
n.toFixed(2); // 1.00

Here we pass the value 2 into our toFixed() method to tell it we want a result to 2 decimal places.

The problem with this however is that it rounds your number to the nearest value. 1.999 would be rounded to 2 (1.999 rounds up to 2.00 instead of rounding down to 1.990 as this is simply how rounding works):

var n = 1.99999;
n.toFixed(2); // 2.00

Your other example would work fine here though:

var n = 8.6543;
n.toFixed(2); // 8.65;

If you definitely want 1.99999 to become 1.99 then please refer to this answer: Display two decimal places, no rounding.

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • Apparently, this is outside of my understanding, because I have been tinkering with this for a while now, and I have looked at the referenced link, and I still can't get it to work. –  Jan 13 '14 at 23:42
0

See it here: http://jsfiddle.net/thetuneupguy/JaCaH/9/

I was able to achieve my need with this...

var change = obj.Change;
var num = parseFloat(change);
var str = num.toFixed(10);
str = str.substring(0, str.length-8);

and...

$tr.append($('<td class="'+changeClass+'">').text(str || "--"));

Now, I need to figure out how to append a + sign to positives.