2014-10-30 79 views
-1

我有從一個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

回答

0

我不知道你正在運行的服務器端代碼,但像這樣的在淨工作:

<%= 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>