“无法从字符串转换为双精度”

时间:2019-06-25 08:39:11

标签: c# string math

请有人可以帮助我解决以下问题。在最后一行中,我收到一条错误消息,指出我

  

无法从字符串转换为双精度

尽管可以在前两行中使用。帮助将不胜感激。

<script>
mapboxgl.accessToken = &#39;........&#39;;

var map = new mapboxgl.Map({
container: &#39;map&#39;,
style: &#39;mapbox://styles/mapbox/streets-v11&#39;,
center: [25.147437, 37.548452],
zoom: 9.5
});

// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());


map.on(&#39;load&#39;, function () {

var location = document.getElementById(&quot;1234&quot;).innerText;
var locations= eval(&#39;[&#39;+location+&#39;];&#39;);
// Add a layer showing the places.
map.addLayer({
&quot;id&quot;: &quot;places&quot;,
&quot;type&quot;: &quot;symbol&quot;,
&quot;source&quot;: {&quot;type&quot;: &quot;geojson&quot;,&quot;data&quot;: {&quot;type&quot;: &quot;FeatureCollection&quot;,&quot;features&quot;: locations}},&quot;layout&quot;: {&quot;icon-image&quot;: &quot;{icon}-15&quot;,&quot;icon-allow-overlap&quot;: true}});

// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on(&#39;click&#39;, &#39;places&#39;, function (e) {
var coordinates = e.features[0].geometry.coordinates.slice();

// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) &gt; 180) {
coordinates[0] += e.lngLat.lng &gt; coordinates[0] ? 360 : -360;
}

new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML('<b>'+ e.features[0].properties.title + '</b>' + e.features[0].properties.description + '<p><img src='+e.features[0].properties.image+'></img></p>')
.addTo(map);

});

// Change the cursor to a pointer when the mouse is over the places layer.
map.on(&#39;mouseenter&#39;, &#39;places&#39;, function () {
map.getCanvas().style.cursor = &#39;pointer&#39;;
});

// Change it back to a pointer when it leaves.
map.on(&#39;mouseleave&#39;, &#39;places&#39;, function () {
map.getCanvas().style.cursor = &#39;&#39;;
});
});

</script>

4 个答案:

答案 0 :(得分:1)

为了检测错误的格式,您可以尝试TryParse方法(在您的情况下为double.TryParse

double y1 = 0;
double x1 = 0;
double y2 = 0;
double x2 = 0;

if (!double.TryParse(YCrd1.Text, out y1)) 
  Obs1Angle.Text = "Incorrect YCrd1 value";
else if (!double.TryParse(YCrd2.Text, out y2)) 
  Obs1Angle.Text = "Incorrect YCrd2 value";
else if (!double.TryParse(XCrd1.Text, out x1)) 
  Obs1Angle.Text = "Incorrect XCrd1 value";
else if (!double.TryParse(XCrd2.Text, out x2)) 
  Obs1Angle.Text = "Incorrect XCrd2 value";
else {
  // All values (x1, x2, y1, y2) are parsed
  DeltaX.Text = $"{x2 - x1}";
  DeltaY.Text = $"{y2 - y1}";

  //DONE: If you want to compute Angle, you want Arc function (ASin, ATan, ACos etc.)
  // Please, note, Atan2 - in case x2 - x1 == 0
  Obs1Angle.Text = $"{Math.Atan2(y2 - y1, x2 - x1)}";
}

答案 1 :(得分:0)

尝试输入代码时,我收到错误消息:

  

CS1503参数1:无法从“十进制”转换为“双精度”

如果查看Math.Tan的文档,您会发现它以double作为输入参数,而不是十进制!因此,您需要进行其他转换:

编辑:此外,您选择了错误的方法。看来您想要一个角度。在这种情况下,您需要使用以参数为参数的Atan方法:

  

代表切线的数字。

并返回:

  

以弧度为单位的角度θ,使-π/ 2≤θ≤π/ 2。

Obs1Angle.Text = Math.Atan((Convert.ToDouble(DeltaY.Text)) / (Convert.ToDouble(DeltaX.Text))).ToString();

答案 2 :(得分:0)

您应使用TryParse来防止您的字符串不是数字。

decimal DeltaYVal, DeltaXVal;
if (decimal.TryParse(DeltaY.Text, out DeltaYVal) && decimal.TryParse(DeltaY.Text, out DeltaXVal))
{
                Obs1Angle.Text = Math.Tan(DeltaYVal / DeltaXVal).ToString();
}

答案 3 :(得分:0)

转换为Double并执行一次。

var deltaY = Convert.ToDouble(YCrd2.Text) - Convert.ToDouble(YCrd1.Text);
var deltaX = Convert.ToDouble(XCrd2.Text) - Convert.ToDouble(XCrd1.Text);

DeltaY.Text = deltaY.ToString();
DeltaX.Text = deltaX.ToString();

Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();

很多整洁的代码,不是吗?

或者,如果要进行解析,请使用C#7.0 ...

if (double.TryParse(YCrd2.Text, out double yCrd2) &&
    double.TryParse(YCrd1.Text, out double yCrd1) &&
    double.TryParse(XCrd2.Text, out double xCrd2) &&
    double.TryParse(XCrd1.Text, out double xCrd1))
{
    var deltaY = yCrd2 - yCrd1;
    var deltaX = xCrd2 - xCrd1;

    DeltaY.Text = deltaY.ToString();
    DeltaX.Text = deltaX.ToString();

    Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();
}
else
{
    // show error in string to double conversion
}

PS:正如@DmitryBychenko在评论中指出的那样,如果Math.Atan2可以为0,请使用Math.Tan而不是DeltaX.Text