关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 float是用于声明变量的关键字,该变量可以存储从±1.5 x 10 -45到±3.4 x 10 38的范围内的浮点值。它是System.Single的别名。
句法:
float variable_name = value;
float关键字在内存中占用4个字节(32位)的空间。我们使用带有值的后缀f或F表示浮点值。
例子:
Input: -3629.4586F
Output: num: -3629.458
Size of a float variable: 4
Input: 16345.6456f
Output: Type of num: System.Single
num: 16345.65
Size of a float variable: 4
Min value of float: -3.402823E+38
Max value of float: 3.402823E+38
范例1:
// C# program for float keyword
using System;
using System.Text;
class GFG {
static void Main(string[] args)
{
// variable declaration
float num = -3629.4586F;
// to print value
Console.WriteLine("num: " + num);
// to print size
Console.WriteLine("Size of a float variable: " + sizeof(float));
}
}
输出:
num: -3629.458
Size of a float variable: 4
范例2:
// C# program for float keyword
using System;
using System.Text;
namespace geeks {
class GFG {
static void Main(string[] args)
{
// variable declaration
float num = 16345.6456f;
// to print type of variable
Console.WriteLine("Type of num: " + num.GetType());
// to print value
Console.WriteLine("num: " + num);
// to print size
Console.WriteLine("Size of a float variable: " + sizeof(float));
// to print minimum & maximum value of float
Console.WriteLine("Min value of float: " + float.MinValue);
Console.WriteLine("Max value of float: " + float.MaxValue);
}
}
}
输出:
Type of num: System.Single
num: 16345.65
Size of a float variable: 4
Min value of float: -3.402823E+38
Max value of float: 3.402823E+38