sunskin
sunskin

Reputation: 1680

How to use Sytem.Math.Round(double, int) in c# (chtml)

    @foreach (var item in ViewBag.OrgTable.Rows)
    {
       @foreach (var p in (List<MvcProject.Models.orgModel>)ViewBag.params)
        { 

          decimal temp = Math.Round(item[p.Abbreviation], (int)p.Count);
          @(temp)

         }
    }

Using the above line of code gave me the error below -

The best overloaded method match for 'System.Math.Round(double, int)' has some invalid arguments

In the above, Abbreviation is a string and Count is a decimal. I am not sure what is wrong with the above System.Math.Round(double,int) usage

Upvotes: 1

Views: 3253

Answers (3)

Kent Skinner
Kent Skinner

Reputation: 116

If item is a DataRow, then the following should work:

Math.Round((double)item[p.Abbreviation], (int)p.Count);

Upvotes: 3

Chris
Chris

Reputation: 4671

Abbreviation is a string and Count is a decimal. I am not sure what is wrong with the above System.Math.Round(double,int) usage

You've just answered your own question. The method expects a double and an int, and you're trying to hand it a string and a decimal. You're going to have to convert those types, for example by calling double.Parse(p.Abbreviation) to convert the string to a double.

Upvotes: 2

iefpw
iefpw

Reputation: 7052

Try double.Parse(abbreviation). It needs to be explicitly converted to double.

Upvotes: 2

Related Questions