简体   繁体   English

如何将对象类型转换为float,其中object可以是任何类型的数字,如int,long,float,double等

[英]How to convert object type into float where object can be any type of number like int, long, float, double etc

I am having as stream of objects, which can be either type of number like int, short, long, float etc... What is the best way to convert it into a number. 我有对象流,可以是int,short,long,float等数字类型...将它转换为数字的最佳方法是什么。 Best way could be something ToNumber(object oNumber) 最好的方法可能是ToNumber(object oNumber)

// Best magic could be
var number = (somemagic) oNumber;

float operator1 = oNumber is int 
  ? (float)(int)oNumber
  : oNumber is long 
     ? (float)(long)oNumber
     : oNumber is float 
        ? (float)oNumber 
        : (float)(int)oNumber;

assuming float will be able to take all number expect double 假设浮动将能够使所有数字期望加倍

我相信Convert.ToSingle将能够处理你抛出的任何“核心”数字类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 通用类型限制为可比较的基本类型,如 int、float、long 等 - C# generic type limit to comparable primitive types like int, float, long, etc 从int作为对象转换为float需要双播 - Double-casting required to convert from int as object to float 在运行时将字符串类型转换为int,float,decimal等类型。 - convert String Type to int,float,decimal etc Type on runtime C# 有什么方法可以在C#中找到字符串是数字(int,long,float或double)的方法吗? - Is there any way to find string is a number (either int, long, float or double) in C#? 无法将类型&#39;double&#39;隐式转换为&#39;float&#39; - Cannot implicitly convert type 'double' to 'float' 将字符串转换为可空类型(int,double等...) - Convert string to nullable type (int, double, etc…) 如何将float转换为int-类型转换为字符串格式? - How to convert float to int - Type Casting To String Format? 将对象转换为float类型,但它看起来像struct System.Single - Cast an object to a type of float but it looks like struct System.Single 将double [] []类型转换为float - converting type double[][] to float 如何转换<class 'unitsnet.ratio'>到一个浮点型数字进行计算?</class> - How to convert <class 'UnitsNet.Ratio'> to a float type number for calculations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM