javascript - Round all decimals from dynamic JSON to 2 decimal places - Stack Overflow

I am making a table that is pulling data dynamically from a JSON file and I need to be able to change t

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: "",
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
},

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.",
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
},
Share Improve this question edited May 14, 2015 at 21:06 Eswizzle asked May 14, 2015 at 20:50 EswizzleEswizzle 571 gold badge1 silver badge10 bronze badges 7
  • 1 possible duplicate of Round to at most 2 decimal places in JavaScript – BillPull Commented May 14, 2015 at 20:53
  • 1 possible duplicate of JavaScript: formatting number with exactly two decimals – hindmost Commented May 14, 2015 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 Commented May 14, 2015 at 20:59
  • @Eswizzle can you please give an example of the JSON data returned – Jack C Commented May 14, 2015 at 21:02
  • @Jaysbays I edited the question to show the JSON example. – Eswizzle Commented May 14, 2015 at 21:06
 |  Show 2 more ments

1 Answer 1

Reset to default 5

Use the javascript numToFixed function:

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

The result of n will be:

5.57

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415738a4439707.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信