2011-08-04 11 views
1

Googleマップアプリケーションでは以下のようになり、標高をフィートに変換して表示したいのですが、最も近い数値に丸めたり下げたりするにはどうすればいいですか? (小数点以下を切り捨てて)number.toFixed(x)メソッドを試しましたが、何もしていないようです。数字を丸めるjavascript - 数学

function getElevation(event) { 

    var locations = []; 
    var clickedLocation = event.latLng; 
    locations.push(clickedLocation); 
    var positionalRequest = { 'locations': locations } 

    // Initiate the location request 
    elevator.getElevationForLocations(positionalRequest, function(results, status) { 
     if (status == google.maps.ElevationStatus.OK) { 

     // Retrieve the first result 
     if (results[0]) { 

      // Open an info window indicating the elevation at the clicked position 
      infowindow.setContent("The elevation at this point <br/>is " + results[0].elevation*(3.2808399) + " feet."); 
      infowindow.setPosition(clickedLocation); 
      infowindow.open(map); 
     } else { 
      alert("No results found"); 
     } 
     } else { 
     alert("Elevation service failed due to: " + status); 
     } 
    }); 
    } 

答えて

2

あなたは最も近い整数に丸めるしたい場合は、単にMath.round(x)

Math.round(25.9) //returns 26 
Math.round(25.2) //returns 25 
Math.round(-2.58) //returns -3 

を使用また、(常に切り捨て)Math.floorに見てみたいことがあります、とMath.ceil(常に切り上げ)。

私はすべての3つの方法を示していフィドル一緒に入れている:jsFiddle

+0

おかげで男に切り上げた数を返します。 'Math.round(results [0] .elevation *(3.2808399))+"足 "で終わりました。 – RonnieT

1

Math.floor(n)

は数が最も近い整数

Math.ceil(n)

に切り捨てする数は切り上げ返します最も近い整数へ

Math.round(n)

は、最も近い整数