I am having difficulty converting val.distance to a round figure.
the value passed from the server for val.distance is 197.13572611205996. what i want to do is to round it to 197.13 or 197.14. before outputting it. If I run this it breaks the code, however when i run it without trying to round the figure it works.
$.getJSON('http://localhost/locategas/pas-autofind.php?lat='+poslat+'&lon='+poslon+'&jsoncallback=?', function (data) {
var output='<ul data-role="listview" data-filter="true">';
$.each(data.david, function(key,val){
var MYDISTANCE = math.round(val.distance);
output+='<li>';
output += '<a href="#detailPage" onclick="showPost(' + val.stationId + ')">';
output+='<h3>' + val.stationName + '</h3>';
output+='<p>' + val.region + '</p>';
output+='<p>' + val.city + '</p>';
output+='<p>' + val.town + '</p>';
output+='<p>' + val.email + '</p>';
output+='<p>' + val.tel1 + '</p>';
output+='<p>' + val.tel2 + '</p>';
output+='<p>' + val.status + '</p>';
output+='<p>' + MYDISTANCE + '</p>';
output+='</a>';
output+='</li>';
}); // go through each post
output+='</ul>';
$('#ul-li-items').html(output);
$('#ul-li-items').trigger('create');
// lists all the posts
});
Any help would be greatly appreciated, thanks.
(Number(8.2342342)).toFixed(2)
Edit: Purpose was casting therefore only Number() is required and not new Number()
reference: new Number() vs Number()
It's hard to tell without seeing your error message, but I imagine that
math.round(…)
should be
Math.round(…)
But this is only the first part of your problem, because Math.round
will round to an integer, not to 2 decimal places.
galchen's answer is a simpler way to achieve what you're trying to do.
Why don't you do something like"
var value=123.123456;
value=value.toFixed(2);
This should give you the value 123.12.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.