Math.Ceiling Method (Double)

.NET Framework Class Library
Math.Ceiling Method (Double)

Returns the smallest integer greater than or equal to the specified double-precision floating-point number. 

C#
<span style="color: blue;">public</span> <span style="color: blue;">static</span> double Ceiling (
    double a
)
<h4 class="subHeading" style="font-size: 1.2em; font-weight: bold; padding: 0px; margin: 0px;">Parameters</h4><dl paramname="a"><dt><span class="parameter"><em>a</em></span> </dt><dd><p>A double-precision floating-point number. </p></dd></dl><p> </p><p> </p><h4 class="subHeading" style="font-size: 1.2em; font-weight: bold; padding: 0px; margin: 0px;">Return Value</h4>The smallest integer greater than or equal to <span class="parameter"><em>a</em></span>. If <span class="parameter"><em>a</em></span> is equal to <a target=_blank id="ctl00_rs1_mainContentContainer_ctl18" style="color: rgb(51, 102, 153); text-decoration: none;" href="http://msdn2.microsoft.com/en-us/library/system.double.nan(VS.80).aspx">NaN</a>, <a target=_blank id="ctl00_rs1_mainContentContainer_ctl19" style="color: rgb(51, 102, 153); text-decoration: none;" href="http://msdn2.microsoft.com/en-us/library/system.double.negativeinfinity(VS.80).aspx">NegativeInfinity</a>, or <a target=_blank id="ctl00_rs1_mainContentContainer_ctl20" style="color: rgb(51, 102, 153); text-decoration: none;" href="http://msdn2.microsoft.com/en-us/library/system.double.positiveinfinity(VS.80).aspx">PositiveInfinity</a>, that value is returned. 
// This example demonstrates Math.Ceiling()
//                           Math.Floor()
using System;
class Sample 
{
    public static void Main() 
    {
    double value;
    decimal idx;
    string nl = Environment.NewLine;
// Return the smallest whole number greater than or equal to the specified number.
// Note that the floating point parameter used in the Math members should not also 
// be used as the loop index. The reason is the loop will not terminate as expected 
// because a floating point number only approximates a decimal number.  
// For the sake of generalizing this example, the decimal index is converted to a 
// floating point number with the Convert class instead of a programming language-
// specific type conversion.
    Console.WriteLine("{0}Ceiling:", nl);
    for (idx = 0.0m; idx <= 1.1m; idx += 0.1m)
        {
        value = Convert.ToDouble(idx);
        Console.WriteLine("Ceiling({0:f}) = {1}", value, Math.Ceiling(value));
        }
// Return the largest whole number less than or equal to the specified number.
    Console.WriteLine("{0}Floor:", nl);
    for (idx = 2.1m; idx >= 1.0m; idx -= 0.1m)
        {
        value = Convert.ToDouble(idx);
        Console.WriteLine("Floor({0:f})   = {1}", value, Math.Floor(value));
        }
    }
}
/*
This example produces the following results:
Ceiling:
Ceiling(0.00) = 0
Ceiling(0.10) = 1
Ceiling(0.20) = 1
Ceiling(0.30) = 1
Ceiling(0.40) = 1
Ceiling(0.50) = 1
Ceiling(0.60) = 1
Ceiling(0.70) = 1
Ceiling(0.80) = 1
Ceiling(0.90) = 1
Ceiling(1.00) = 1
Ceiling(1.10) = 2
Floor:
Floor(2.10)   = 2
Floor(2.00)   = 2
Floor(1.90)   = 1
Floor(1.80)   = 1
Floor(1.70)   = 1
Floor(1.60)   = 1
Floor(1.50)   = 1
Floor(1.40)   = 1
Floor(1.30)   = 1
Floor(1.20)   = 1
Floor(1.10)   = 1
Floor(1.00)   = 1
*/
發佈了5 篇原創文章 · 獲贊 5 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章