将数字舍入到小数位

问题描述:

我有从一个api中读取的lat lng坐标,所以我可以将它们放在一个跨度中。我遇到的问题是,坐标是通过api输入的任何值,然后我将它们输出到页面上。将数字舍入到小数位

<div id="co-ordinates"> 
<strong>Coordinates</strong> 
<span class="lat"><%= @camera.deep_fetch(:location, :lat) {} %></span> 
<span class="lng"><%= @camera.deep_fetch(:location, :lng) {} %></span> 
</div> 

有没有办法那么我可以使用JavaScript来的坐标值的小数位数限制为6位?

+0

'13.37.toFixed(6)' – A1rPun 2014-10-30 14:18:42

+1

Igor 2014-10-30 14:20:44

我不知道你正在运行的服务器端代码,但像这样的在净工作:

<%= Math.Round(@camera.deep_fetch(:location, :lat) {}, 6) %> 

如果不工作,你可以编写服务器端功能调用@camera并获得它的经度和长度 - 然后返回它们,四舍五入到小数点后6位。事情是这样的:

服务器端(这些都需要市民):您将需要写对getCamera ...功能,很明显:

public decimal LatRounded(int decimalPlaces) 
{ 
    return Math.Round(GetCameraLatFunction(), decimalPlaces); 
} 

public decimal LongRounded(int decimalPlaces) 
{ 
    return Math.Round(GetCameraLongFunction(), decimalPlaces); 
} 

然后,你想从你的网页像这样调用这个:

<div id="co-ordinates"> 
<strong>Coordinates</strong> 
<span class="lat"><%= LatRounded(6) %></span> 
<span class="lng"><%= LongRounded(6) %></span> 
</div>