標籤

more tags...

最新回覆

[C#]利用Single.IsNaN來判斷NaN

最近在寫一些Report的東西,

Report常常會有一些計算公式,例如a/b

結果出現了"NaN"的東西,要如何解決呢,直接看範例吧..

c#

using System;

namespace CSharpConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            float a;
            float b;


            //NaN,不是一個數字
            a = 0;
            b = 0;
            Console.WriteLine(a / b);
            Console.WriteLine(Single.IsNaN(a / b) ? "0" : (a / b).ToString());


            //PositiveInfinity,正無窮大
            a = 1;
            b = 0;
            Console.WriteLine(a / b);
            Console.WriteLine(Single.IsPositiveInfinity(a / b) ? "0" : (a / b).ToString());


            //NegativeInfinity,負無窮大
            a = -1;
            b = 0;
            Console.WriteLine(a / b);
            Console.WriteLine(Single.IsNegativeInfinity(a / b) ? "0" : (a / b).ToString());


            Console.ReadLine();
        }
    }
}


執行結果:

參考網址:

http://msdn.microsoft.com/zh-tw/library/system.single.nan.aspx

 

推到 Twitter!
推到 Plurk!


 

2009/11/12 11:32| 閱讀數 : 1176 | 1 人收藏 我要推薦 | Add Comment | 文章分類 : C#(WinForm) 訂閱


回覆

目前沒有回應.

發表回覆

標題: *
姓名: *
Email: (將不會被顯示)
Url:
回覆: *
登入後使用進階評論
Please add 4 and 1 and type the answer here:
F6 Team logo


用BloggerAds 替自已加薪

每月文章

文章分類

推薦討論區

推薦部落格