本文介绍了舍入错误从float转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我刚刚发现了C#中发生的舍入错误。我很确定

这是一个老问题,但这对我来说是新的,并且导致了相当多的时间...... b $ b时间试图追踪这个问题。 />

基本上将以下代码放入C#应用程序:

float testFloat2 =(int)(4.2f *(float)100);

Console.Out.WriteLine(" 1:" + testFloat2);


,结果将是419


如果我使用Convert.ToInt32我可以解决这个问题 - 但似乎

两者都应该做同样的事情,不应该吗?


新手

Hi all, I just discovered a rounding error that occurs in C#. I''m sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.

Basically put the following code into your C# app:
float testFloat2 = (int) (4.2f * (float)100);
Console.Out.WriteLine("1: "+testFloat2);

and the result will be 419

If I use Convert.ToInt32 I can get around this problem - but it seems
that both should do the same thing, shouldn''t they?

Novice

推荐答案









http://www.math.grin.edu/~stone/cour...EEE-reals.html



这篇关于舍入错误从float转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:18