使用浮点值 [英] Working with floating point values

查看:65
本文介绍了使用浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用浮点值(双打)的项目,而且我很可能被一些非常基本的东西所驱使。据我所知,浮点计算的性质是产生价值,比如

0.10000000000000003,当我真正想要的是0.1时。但有没有什么方法可以在最后消除这个数字?我已经尝试过四舍五入,但这只是将数字移动到最不重要的位置,例如0.1000003。


如果不这样做,任何人都可以推荐任何用于处理

浮点问题的优秀在线文章,除了使用Epsilon进行零比较之外?谢谢

预付款。


David Veeneman

远见系统

I''m working on a project that uses floating-point values (doubles), and I''m
being driven crazy by something pretty basic. I understand that it''s in the
nature of floating-point calculations to produce values like
0.10000000000000003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I''ve tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems

推荐答案

David,


我已经快速解决了你的问题而且Math.Round方法似乎确实需要工作



static void Main(string [] args)

{


double TestNumber = 0.100000000003;


Console.WriteLine(TestNumber.ToString());


Console.WriteLine(Math.Round(TestNumber,1));

Console.WriteLine(Math.Round(TestNumber,5));

Console.ReadLine();

}


输出:


0.100000000003

0.1

0.1


希望这有帮助


David Veeneman写道:
David,

I''ve had a quick go at your problem and the Math.Round method does seem
to work.

static void Main(string[] args)
{

double TestNumber = 0.100000000003;

Console.WriteLine ( TestNumber.ToString() );

Console.WriteLine ( Math.Round ( TestNumber, 1 ) );
Console.WriteLine ( Math.Round ( TestNumber, 5 ) );
Console.ReadLine();
}

OUTPUT:

0.100000000003
0.1
0.1

Hope this helps

David Veeneman wrote:
我正在研究一个使用浮点值(双打)的项目,我是<被一些非常基本的东西驱使疯狂。据我所知,当我真正想要的是0.1时,浮点计算的性质就是产生像 0.10000000000000003这样的值。但有没有办法在最后消除这个数字?我已经尝试过四舍五入,但这只是将数字移动到最不重要的位置,例如0.1000003。

如果不这样,任何人都可以推荐任何好的在线文章来处理提前。

David Veeneman
远见系统
I''m working on a project that uses floating-point values (doubles), and I''m
being driven crazy by something pretty basic. I understand that it''s in the
nature of floating-point calculations to produce values like
0.10000000000000003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I''ve tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems






只需使用十进制。

-

Brian Delahunty

爱尔兰

http://briandela.com/blog

" David Veeneman"写道:
Just use Decimal.
--
Brian Delahunty
Ireland

http://briandela.com/blog
"David Veeneman" wrote:
我正在研究一个使用浮点值(双打)的项目,而且我被一些非常基本的东西所驱使。据我所知,当我真正想要的是0.1时,浮点计算的性质就是产生像 0.10000000000000003这样的值。但有没有办法在最后消除这个数字?我已经尝试过四舍五入,但这只是将数字移动到最不重要的位置,例如0.1000003。

如果不这样,任何人都可以推荐任何好的在线文章来处理提前。

David Veeneman
远见系统
I''m working on a project that uses floating-point values (doubles), and I''m
being driven crazy by something pretty basic. I understand that it''s in the
nature of floating-point calculations to produce values like
0.10000000000000003, when what I really want is 0.1. But is there any way to
eliminate that digit at the end? I''ve tried rounding, but that simply moves
the digit to the least significant position, such as 0.1000003.

Failing that, can anyone recommend any good online articles for dealing with
floating-point issues, beyond using Epsilon for zero comparisons? Thanks in
advance.

David Veeneman
Foresight Systems



你可以格式化数字如

String.Format(" {0:#,## 0.00;#,## 0.00; 0.00}",myDecimal.ToString());

(假设你最后想要一个字符串)


Ranjeet。
you could format the number like
String.Format("{0:#,##0.00;#,##0.00;0.00}", myDecimal.ToString()) ;
(assuming you want a string at the end)

Ranjeet.


这篇关于使用浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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