指定的转换是无效的。如何解决这个 [英] Specified cast is not valid.. how to resolve this

查看:148
本文介绍了指定的转换是无效的。如何解决这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能

public object Convert(object value)
 {
    string retVal = string.Empty;
    int oneMillion = 1000000;
    retVal = ((double)value / oneMillion).ToString("###,###,###.###");
    return retVal;
 }



我调用像

I am invoking like

var result  = Convert(107284403940);



错误:规定的强制转换无效

如何解决...

请注意:〜对象的值可以是双,十进制浮点,整数(32 64)..什么

Note:~ the object value can be double, decimal, float, integer(32 and 64)..anything

是否有可能在运行时做类型转换?

Is it possible to do the typecasting at runtime?

推荐答案

使用 Convert.ToDouble(值),而不是(双)值。它需要一个对象,并支持所有你要的类型! :)

Use Convert.ToDouble(value) rather than (double)value. It takes an object and supports all of the types you asked for! :)

另外,你的方法总是返回字符串在上面的代码;我建议具有方法提示并给它一个更明显的名称(公共字符串FormatLargeNumber(对象的值)

Also, your method is always returning a string in the code above; I'd recommend having the method indicate so, and give it a more obvious name (public string FormatLargeNumber(object value))

这篇关于指定的转换是无效的。如何解决这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆