Specified cast is not valid.. how to resolve this

后端 未结 2 1293
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 14:19

I have the below function

public object Convert(object value)
 {
    string retVal = string.Empty;
    int oneMillion = 1000000;
    retVal = ((double)value          


        
相关标签:
2条回答
  • 2020-12-09 15:08

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

    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))

    0 讨论(0)
  • 2020-12-09 15:13

    If you are expecting double, decimal, float, integer why not use the one which accomodates all namely decimal (128 bits are enough for most numbers you are looking at).

    instead of (double)value use decimal.Parse(value.ToString()) or Convert.ToDecimal(value)

    0 讨论(0)
提交回复
热议问题