locked
Math.Round Error RRS feed

  • Question

  • User-1753299103 posted

    I am using the following to round a number without any decimails showing. Midpoint to the the next number i.e 1.5 =>2

    @Math.Round(finalscore)
    

    I am getting the following error:

    The call is ambiguous between the following methods or properties: 'System.Math.Round(double)' and 'System.Math.Round(decimal)'

     

    Friday, May 31, 2013 7:41 PM

Answers

  • User1713851783 posted

    Use

    @Math.Round(73 / 3d)

    Pay attention to "3d" instead of "3" as stated in C# The call is ambiguous between the following methods or properties: 'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 3:53 PM
  • User281315223 posted

    The Math.Round() method actually accepts additional parameters that handle things like Midpoint Rounding rules that you may want to look into, however it should be working properly if you specify the value provided as a decimal or a double : 

    @{Decimal finalscore = 1.5m;}
    
    <!-- This will yield 2 -->
    @Math.Round(finalscore)

    you could handle this by simply casting your finalscore variable as well : 

    @{var finalscore = 1.5;}
    
    <!-- This will yield 2 -->
    @Math.Round((double)finalscore)

    Since you are actually evaluating an expression within your Round method, you'll need to explicitly set the type of the value being processed within it : 

    @Math.Round((double)73 / 3)

    or you could also handle this by simply using the shorthand identifer for a datatype such as m, f, or d : 

    @Math.Round(73d / 3)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 4:00 PM
  • User-1753299103 posted

    Just an added update that your variable must initially be declared  double:

    double mynymber = 0;

    @Math.Round((double)mynymber / 3);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2013 2:16 PM

All replies

  • User-1716253493 posted
    use double type finalscore. finalscore= Math .Round(finalscore)
    Friday, May 31, 2013 7:55 PM
  • User-2005691517 posted

    What is the data type of finalscore? How are you calculating it?

    Make sure that finalscore is of type decimal (or double).

    Friday, May 31, 2013 8:11 PM
  • User-2121587974 posted

    just cast finalscore as a double.

    @Math.Round((double)finalscore)

    Saturday, June 1, 2013 2:46 AM
  • User-1980594115 posted
        @{
            decimal amount = 0.685M;
        
            decimal rounded3 = decimal.Round(amount, 2, MidpointRounding.AwayFromZero); 
        
            @rounded3 <br />
        }
    
        // Displayed as:
        //                    0.69 
    

    Tuesday, June 4, 2013 3:00 PM
  • User-1753299103 posted

    What I am looking for is:

    73 / 3 = 24.333 rounded to 24 and let say if the resut is 24.512 then rounded to 25

     I don't need to display any decimals.

    Tuesday, June 4, 2013 3:10 PM
  • User1713851783 posted

    Use

    @Math.Round(73 / 3d)

    Pay attention to "3d" instead of "3" as stated in C# The call is ambiguous between the following methods or properties: 'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 3:53 PM
  • User281315223 posted

    The Math.Round() method actually accepts additional parameters that handle things like Midpoint Rounding rules that you may want to look into, however it should be working properly if you specify the value provided as a decimal or a double : 

    @{Decimal finalscore = 1.5m;}
    
    <!-- This will yield 2 -->
    @Math.Round(finalscore)

    you could handle this by simply casting your finalscore variable as well : 

    @{var finalscore = 1.5;}
    
    <!-- This will yield 2 -->
    @Math.Round((double)finalscore)

    Since you are actually evaluating an expression within your Round method, you'll need to explicitly set the type of the value being processed within it : 

    @Math.Round((double)73 / 3)

    or you could also handle this by simply using the shorthand identifer for a datatype such as m, f, or d : 

    @Math.Round(73d / 3)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2013 4:00 PM
  • User-1753299103 posted

    Just an added update that your variable must initially be declared  double:

    double mynymber = 0;

    @Math.Round((double)mynymber / 3);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2013 2:16 PM
  • User-1980594115 posted
    @{
        decimal amount = 73 / 3;
        decimal rounded3 = decimal.Round(amount, 0, MidpointRounding.AwayFromZero);
    }

    Wednesday, June 5, 2013 4:57 PM