Convert float value to string

主宰稳场 提交于 2019-12-11 23:27:30

问题


I have a property like this:

 public float Lat{
        get {
            float lat;
            if (!float.TryParse(hdnLat.Value, out lat))  
            {
                throw new NotImplementedException();
            }

            return lat;
        }
        set { 
            hdnLat.Value = value;  // Line 43
        }
    }

I got Latitude and Longitude from Google Maps and i get the cordinates from two asp.net hiddenfields.

<asp:HiddenField ID="hdnLat" runat="server" />
<asp:HiddenField ID="hdnLng" runat="server" />

I store my latitude and longitude as float in my databas so i have to convert it to float right?

How can i convert my cordinates to correct format?

Visual Studio givs me this error:

Can not implicitly convert type double to string Line 43

How can i solve this problem?


回答1:


Since hdnLat.Value is of type string, when you assign to it, the item you assign must also by of type string. So if you want to assign value, you have to convert it into a comparable item of type string. And you can do that like this:

hdnLat.Value = value.ToString();

Which is exactly what the error message "can not implicitly convert type double to string" is trying to tell you. You should read this message as "I see you're trying to use a double where I expected a string. I tried to figure out how to convert it, but I cannot. Could you tell me explicitly how to convert it?"



来源:https://stackoverflow.com/questions/6016689/convert-float-value-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!