Error "cannot implicitly convert type string to decimal"

时间:2017-12-18 07:26:59

标签: c#

am trying to get my values displayed in the form of currency.i have a code that am making use of which should convert string to decimal.am getting the error that it can not convert the string to decimal.the error is at the line of code where i have string.format.

string[] x = new string[dt.Rows.Count];

decimal[] y = new decimal[dt.Rows.Count];

for (int i = 0; i<dt.Rows.Count; i++)
{
    x[i] = dt.Rows[i][0].ToString();
    y[i] = string.Format("{0:##,##0.00}",Convert.ToInt32(dt.Rows[i][1]));
}

2 个答案:

答案 0 :(得分:1)

Just change:

l

to:

decimal[] y = new decimal[dt.Rows.Count];

and use the following conversion:

string[] y = new string[dt.Rows.Count];

Alternatively, if you dont't want to change the type of y[i] = string.Format("{0:##,##0.00}", dt.Rows[i][1].ToString()); :

y

With this last approach, if you need more control in order to deal with the current numbers format, use this version of y[i] = Decimal.Parse(dt.Rows[i][1]); : https://msdn.microsoft.com/en-us/library/s84kdbzx(v=vs.110).aspx

答案 1 :(得分:0)

You try to put a string in a decimal which is wrong. You should use the default currency formater and change your decimal in string :

func isNotNil(someObject: Any?) -> Bool {
        if someObject is String {
            if (someObject as? String) != nil {
                return true
            }else {
                return false
            }
        }else if someObject is Array<Any> {
            if (someObject as? Array<Any>) != nil {
                return true
            }else {
                return false
            }
        }else if someObject is Dictionary<AnyHashable, Any> {
            if (someObject as? Dictionary<String, Any>) != nil {
                return true
            }else {
                return false
            }
        }else if someObject is Data {
            if (someObject as? Data) != nil {
                return true
            }else {
                return false
            }
        }else if someObject is NSNumber {
            if (someObject as? NSNumber) != nil{
                return true
            }else {
                return false
            }
        }else if someObject is UIImage {
            if (someObject as? UIImage) != nil {
                return true
            }else {
                return false
            }
        }
        return false
 }