C# 中的 Single.ToString() 方法用于将此实例的数值转换为其等效的字符串表示形式。
用法
语法如下 -
public override string ToString ();
示例
现在让我们看一个例子 -
using System;
public class Demo {
public static void Main(){
float f1 = 10.0f/0.0f;
float f2 = -3.0f;
Console.WriteLine("Value1 (String representation) = "+f1.ToString());
Console.WriteLine("Hashcode for Value1 = "+f1.GetHashCode());
Console.WriteLine("TypeCode for Value1 = "+f1.GetTypeCode());
Console.WriteLine("Is Value1 value is positive or negative infinity? = "+Single.IsInfinity(f1));
Console.WriteLine("Is Value1 NaN? = "+Single.IsNaN(f1));
Console.WriteLine("Is Value1 a positive infinity? = "+Single.IsPositiveInfinity(f1));
Console.WriteLine("\nValue1 (String representation) = "+f2.ToString());
Console.WriteLine("Hashcode for Value2 = "+f2.GetHashCode());
Console.WriteLine("TypeCode for Value2 = "+f2.GetTypeCode());
Console.WriteLine("Is Value2 value is positive or negative infinity? = "+Single.IsInfinity(f2));
Console.WriteLine("Is Value2 NaN? = "+Single.IsNaN(f2));
Console.WriteLine("Is Value2 a positive infinity? = "+Single.IsPositiveInfinity(f2));
}
}
输出
这将产生以下输出 -
Value1 (String representation) = ∞ Hashcode for Value1 = 2139095040 TypeCode for Value1 = Single Is Value1 value is positive or negative infinity? = True Is Value1 NaN? = False Is Value1 a positive infinity? = True Value1 (String representation) = -3 Hashcode for Value2 = -1069547520 TypeCode for Value2 = Single Is Value2 value is positive or negative infinity? = False Is Value2 NaN? = False Is Value2 a positive infinity? = False
示例
现在让我们看另一个例子 -
using System;
public class Demo {
public static void Main(){
float f1 = 5.0f/0.0f;
float f2 = 0.0f/0.0f;
Console.WriteLine("Value1 (String representation) = "+f1.ToString());
Console.WriteLine("Hashcode for Value1 = "+f1.GetHashCode());
Console.WriteLine("TypeCode for Value1 = "+f1.GetTypeCode());
Console.WriteLine("Is Value1 value is positive or negative infinity? = "+Single.IsInfinity(f1));
Console.WriteLine("Is Value1 NaN? = "+Single.IsNaN(f1));
Console.WriteLine("Is Value1 a negative infinity? = "+Single.IsNegativeInfinity(f1));
Console.WriteLine("\nValue2 (String representation) = "+f2.ToString());
Console.WriteLine("Hashcode for Value2 = "+f2.GetHashCode());
Console.WriteLine("TypeCode for Value2 = "+f2.GetTypeCode());
Console.WriteLine("Is Value2 value is positive or negative infinity? = "+Single.IsInfinity(f2));
Console.WriteLine("Is Value2 NaN? = "+Single.IsNaN(f2));
Console.WriteLine("Is Value2 a negative infinity? = "+Single.IsNegativeInfinity(f2));
}
}
输出
这将产生以下输出 -
Value1 (String representation) = ∞ Hashcode for Value1 = 2139095040 TypeCode for Value1 = Single Is Value1 value is positive or negative infinity? = True Is Value1 NaN? = False Is Value1 a negative infinity? = False Value2 (String representation) = NaN Hashcode for Value2 = -4194304 TypeCode for Value2 = Single Is Value2 value is positive or negative infinity? = False Is Value2 NaN? = True Is Value2 a negative infinity? = False
相关用法
- C# Single.ToString()函数用法及代码示例
- C# Single.IsNegative()用法及代码示例
- C# Single.GetTypeCode用法及代码示例
- C# Single.IsNaN()用法及代码示例
- C# Single.GetHashCode()用法及代码示例
- C# Single.IsPositiveInfinity()用法及代码示例
- C# Single.CompareTo()用法及代码示例
- C# Single.Equals()用法及代码示例
- C# Single.IsNegativeInfinity()用法及代码示例
- C# Single.IsFinite()用法及代码示例
- C# Single.IsInfinity()用法及代码示例
- C# String.ToUpperInvariant用法及代码示例
- C# StringBuilder.ToString用法及代码示例
- C# Stack.Contains()用法及代码示例
- C# String ToString()用法及代码示例
- C# String Contains()用法及代码示例
- C# String ToCharArray()用法及代码示例
- C# SortedDictionary.Remove()用法及代码示例
- C# String IndexOf()用法及代码示例
- C# String TrimEnd()用法及代码示例
注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 Single.ToString Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。