Csharp/CSharp Tutorial/Data Type/float

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Float Literals

using System;
class MainClass
{
   static void Main()
   {
      Console.WriteLine("{0}", 3.1416F);     // float literal
   }
}
3.1416

floats and arithmetic operators

class MainClass
{
  public static void Main()
  {
    
    System.Console.WriteLine("10f / 3f = " + 10f / 3f);
    float floatValue1 = 10f;
    float floatValue2 = 3f;
    System.Console.WriteLine("floatValue1 / floatValue2 = " + floatValue1 / floatValue2);
  }
}
10f / 3f = 3.333333
floatValue1 / floatValue2 = 3.333333

Pass float to a function

using System;
class MainClass
{
   public static void Main() {
      float f = 6.0F;
      
      float s = Avg(5.0F, f);
      
      Console.WriteLine(s);        
   }
   public static float Avg(float Input1, float Input2)      
   {
      return (Input1 + Input2) / 2.0F;               
   }
}
5.5

Use Remainder Operator on float point data type

using System;
class MainClass
{
   static void Main()
   {
      Console.WriteLine("0.0f % 1.3f is {0}", 0.0f % 1.3f);
      Console.WriteLine("0.3f % 1.3f is {0}", 0.3f % 1.3f);
      Console.WriteLine("1.0f % 1.3f is {0}", 1.0f % 1.3f);
      Console.WriteLine("1.3f % 1.3f is {0}", 1.3f % 1.3f);
      Console.WriteLine("2.0f % 1.3f is {0}", 2.0f % 1.3f);
      Console.WriteLine("2.3f % 1.3f is {0}", 2.3f % 1.3f);
   }
}
0.0f % 1.3f is 0
0.3f % 1.3f is 0.3
1.0f % 1.3f is 1
1.3f % 1.3f is 0
2.0f % 1.3f is 0.7
2.3f % 1.3f is 1