LINQ到SQL奇怪的浮动行为 [英] linq to sql strange float behaviour

查看:131
本文介绍了LINQ到SQL奇怪的浮动行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个double(TaxRate here)的问题,当它通过函数时没有相同的值:

我首先调用一个函数

p>

  DataContext.UpdateCategory(theCategorie.Code,theCategorie.Description,theCategorie.TaxRate,theCategorie.TaxCode); 

运行调试器时,TaxRate的值是 19.6



然后,当我在一个名为dbml的函数中放置一个停止点时:

  [Function(Name =dbo.UpdateCategory)] 
public int UpdateCategory([Parameter(Name =Code,DbType =VarChar(20))] string code,[Parameter(Name = (参数(Name =TaxRate,DbType =Float)]] System.Nullable< double> taxRate,[Parameter(Name =TaxCode,DbType = (),((MethodInfo)(MethodInfo.GetCurrentMethod())),uNSPSC,description,taxRate,taxCode)NChar(10))] string taxCode)
{
IExecuteResult result = this.ExecuteMethodCall ;
return((int)(result.ReturnValue));
}

这里的值是 19.6000003814697 。你看到奇怪的加小数?这两个调用之间没有任何操作,为什么这些小数出现?

解决方案

是theCategorie.TaxRate浮动吗?如果是这样,你将分配一个浮动到双。额外的小数是由于更高的精度在双,所以双最接近浮点数19.6 ...

这个输出说明它:

  float f = 19.6F; 
double d = f;
double d2 = 19.6D;

System.Diagnostics.Debug.WriteLine(Float:+ f.ToString());
System.Diagnostics.Debug.WriteLine(Double from float:+ d.ToString());
System.Diagnostics.Debug.WriteLine(Double:+ d2.ToString());

作为一个解决方法,您可以将Categorie.TaxRate的类型更改为double,或者如果想保持它作为一个浮动,你可以施放&在调用存储过程时调用它:

  DataContext.UpdateCategory(theCategorie.Code,theCategorie.Description,Math.Round((double )theCategorie.TaxRate,6),theCategorie.TaxCode); 


I have a problem with a double (TaxRate here), that do not have the same value when it pass through functions:

I first call a function

DataContext.UpdateCategory(theCategorie.Code, theCategorie.Description, theCategorie.TaxRate, theCategorie.TaxCode);

When I run the debugger the value of TaxRate is 19.6

Then when I put a stop point in the dbml function that is called:

 [Function(Name="dbo.UpdateCategory")]
 public int UpdateCategory([Parameter(Name="Code", DbType="VarChar(20)")] string code, [Parameter(Name="Description", DbType="NVarChar(512)")] string description, [Parameter(Name="TaxRate", DbType="Float")] System.Nullable<double> taxRate, [Parameter(Name="TaxCode", DbType="NChar(10)")] string taxCode)
 {
     IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), uNSPSC, description, taxRate, taxCode);
     return ((int)(result.ReturnValue));
 }

here the value is 19.6000003814697. You see the strange added decimal? there are no operations between these two calls, so why these decimals appears?

解决方案

Is theCategorie.TaxRate a float? If so, you're assigning a float into a double. The extra decimals is due to higher precision in the double, so the double's nearest to a float's 19.6...

The output of this illustrates it:

float f = 19.6F;
double d = f;
double d2 = 19.6D;

System.Diagnostics.Debug.WriteLine("Float: " + f.ToString());
System.Diagnostics.Debug.WriteLine("Double from float: " + d.ToString());
System.Diagnostics.Debug.WriteLine("Double: " + d2.ToString());

As a workaround, you can either change the type of theCategorie.TaxRate to a double, or if you want to keep it as a float you can cast & round it when calling the stored proc:

DataContext.UpdateCategory(theCategorie.Code, theCategorie.Description, Math.Round((double)theCategorie.TaxRate, 6), theCategorie.TaxCode);

这篇关于LINQ到SQL奇怪的浮动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆