将浮点值转换为字符串

将浮点值转换为字符串

本文介绍了将浮点值转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的属性:

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

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

我从Google获得了纬度和经度地图和我从两个asp.net hiddenfields获取坐标。

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" />

我将纬度和经度存储为float到我的数据库中,所以我必须将其转换为float吗?

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会向我显示此错误:

Visual Studio givs me this error:

我该如何解决此问题?

How can i solve this problem?

推荐答案

由于 hdnLat.Value 的类型为 string ,当您分配给它时,您分配的项目还必须具有类型 string 。因此,如果要分配 value ,则必须将其转换为 string 类型的可比较项。您可以这样操作:

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?"

这篇关于将浮点值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:45