C# 浮点数转换错误
我在数据库(餐桌)中定义了纬度浮动(24)和经度浮动(24)。当我尝试使用它们时,我的模型中显示的代码如下C# 浮点数转换错误,c#,asp.net-mvc,casting,C#,Asp.net Mvc,Casting,我在数据库(餐桌)中定义了纬度浮动(24)和经度浮动(24)。当我尝试使用它们时,我的模型中显示的代码如下 public class JsonDinner { public JsonDinner(){} public JsonDinner(Dinner dinner) { DinnerID = dinner.DinnerID; EventDate = dinner.EventDate.ToString(); Latitu
public class JsonDinner
{
public JsonDinner(){}
public JsonDinner(Dinner dinner)
{
DinnerID = dinner.DinnerID;
EventDate = dinner.EventDate.ToString();
Latitude = dinner.Latitude;
Longitude = dinner.Longitude;
Title = dinner.Title;
Description = dinner.Description;
RSVPCount = dinner.RSVPs.Count;
Url = "dinner/details/" + dinner.DinnerID.ToString();
}
public int DinnerID {get; set;}
public string EventDate {get; set;}
public float Latitude {get; set;}
public float Longitude {get; set;}
public string Title {get; set;}
public string Description {get; set;}
public int RSVPCount {get; set;}
public string Url {get; set;}
}
上面说
无法将“float”隐式转换为“float”。显式转换
存在(您是否缺少演员阵容)
我做错了什么?我甚至没有使用任何铸造。从数据库中出来,我相信
float
等同于C#double
。因此,让他们:
Latitude = (float)dinner.Latitude;
Longitude = (float)dinner.Longitude;
但是要明白,你可能会降低精度,因此截断/舍入成为一种真正的可能性。从数据库中出来,我相信
浮点值
等同于C#双精度
。因此,让他们:
Latitude = (float)dinner.Latitude;
Longitude = (float)dinner.Longitude;
但要明白,你可能会降低精度,因此,截断/舍入成为了一种真正的可能性。你得到了错误的哪一行?你也可以在你的
晚餐
类中显示代码吗?你可以尝试将浮点定义为32吗?你的项目中是否碰巧有一个名为浮点
的类?你得到了错误的哪一行?你也可以在晚餐
中显示代码吗类?可以尝试将浮点定义为32吗?您的项目中是否碰巧有一个名为float
的类?它是否会抱怨双/浮点转换?不,因为数据库中的类型是float。看这里:谢谢大家@DonBoitnott的答案是正确的。错误消失了。我为纬度和经度定义了float type,它们等同于double type到C#,所以我需要告诉C#将double number转换为float。但是为什么它们不能以相同的方式一致地工作呢?难道它不会抱怨double/float转换吗?不会,因为数据库中的类型是float。看这里:谢谢大家@DonBoitnott的答案是正确的。错误消失了。我为纬度和经度定义了float type,它们等同于double type到C#,所以我需要告诉C#将double number转换为float。但是为什么他们不一直以同样的方式工作呢