0

I am making a table that is pulling data dynamically from a JSON file and I need to be able to change the value to be at two decimal places. Is this even possible based on the code I already have? Examples of what I want to do:

--7 changes to 7.00

--3922.2 changes to 3922.20

--89.2823 changes to 89.28

Thanks so much in advance!

Here is my HTML:

<table id="reportTable" class="reportTable">
    <th>Timer Name</th>
    <th>Daily Percentile 90th</th>
    <th>Daily Percentile 50th</th>  
    <th>Min Percentile 90th</th>
    <th>Max Percentile 90th</th>
    <th>Daily Average</th>
</table>

Here is my JQuery and Javascript:

//THIS CODE GETS THE JSON, ALPHABETIZES THE INFO, AND POPULATES THE TABLE.
var information = $.ajax({
type: "GET",
url: "http://websiteIgetmyJSONfrom.com",
dataType: "json",
success: function (information) {
    information.sort( function( a, b ) {
        a = a.timerName.toLowerCase();
        b = b.timerName.toLowerCase();

        return a < b ? -1 : a > b ? 1 : 0;
    });

    $.each(information, function(i, item) {
        var $tr = $('<tr class="clickable">').append(
            $('<td align="left">').text(item.timerName),
            $('<td>').text(item.daily_percentile_90th),
            $('<td>').text(item.daily_percentile_50th),             
            $('<td>').text(item.min_percentile_90th),
            $('<td>').text(item.max_percentile_90th),
            $('<td>').text(item.daily_average)).appendTo('#reportTable');
    });
},
error: function(){ alert("FAILED TO LOAD JSON"); }
});

Example of JSON returned:

{
    "daily_percentile_90th": 4430.6,
    "min_percentile_90th": 1598.8,
    "max_percentile_90th": 5518.9,
    "daily_percentile_50th": 3793.5,
    "timerName": "Temple:Shared",
    "daily_average": 3745.16
},
{
    "daily_percentile_90th": 1904.2,
    "min_percentile_90th": 634.4,
    "max_percentile_90th": 3296.6,
    "daily_percentile_50th": 1103.5,
    "timerName": "Search:Catalog",
    "daily_average": 1332.82
},
{
    "daily_percentile_90th": 780,
    "min_percentile_90th": 0.8,
    "max_percentile_90th": 780,
    "daily_percentile_50th": 239,
    "timerName": "FT:Person:Ordinances",
    "daily_average": 397.324
},
Eswizzle
  • 57
  • 1
  • 10
  • 1
    possible duplicate of [Round to at most 2 decimal places in JavaScript](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript) – BillPull May 14 '15 at 20:53
  • 1
    possible duplicate of [JavaScript: formatting number with exactly two decimals](http://stackoverflow.com/questions/1726630/javascript-formatting-number-with-exactly-two-decimals) – hindmost May 14 '15 at 20:55
  • I already tried following these two questions, but neither work for this situation and I just can't seem to figure it out! I think it's probably because of the way I am pulling the information into my table from the JSON. – Eswizzle May 14 '15 at 20:59
  • @Eswizzle can you please give an example of the JSON data returned – Jack C May 14 '15 at 21:02
  • @Jaysbays I edited the question to show the JSON example. – Eswizzle May 14 '15 at 21:06
  • Try converting the JSON to a number first for example Number(item.daily_percentile_90th).toFixed(2) – Jack C May 14 '15 at 21:09
  • Okay I think I jumped the gun.. that didn't change the text to be a number. I tried 'num' 'NumberFormat' 'Number' and 'number' in place of .text and after .text.... any other suggestions? – Eswizzle May 14 '15 at 22:12

1 Answers1

5

Use the javascript numToFixed function:

var num = 5.56789;
var n = num.toFixed(2);

The result of n will be:

5.57
Jack C
  • 1,044
  • 2
  • 12
  • 22