javascript - Round all decimals from dynamic JSON to 2 decimal places -
i making table pulling data dynamically json file , need able change value @ 2 decimal places. possible based on code have? examples of want do:
--7 changes 7.00
--3922.2 changes 3922.20
--89.2823 changes 89.28
thanks in advance!
here 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 jquery , javascript:
//this code gets json, alphabetizes info, , populates table. var information = $.ajax({ type: "get", url: "http://websiteigetmyjsonfrom.com", datatype: "json", success: function (information) { information.sort( function( a, b ) { = a.timername.tolowercase(); b = b.timername.tolowercase(); return < b ? -1 : > 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 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 },
use javascript numtofixed function:
var num = 5.56789; var n = num.tofixed(2);
the result of n be:
5.57
Comments
Post a Comment