chamara
chamara

Reputation: 12711

Math.Round with double value

double? AssessmentTotal=2.345;
double? AssessmentTotalval = Math.Round((decimal)AssessmentTotal == null ? 0 : AssessmentTotal, 2);

I'm trying to Round up a double value. Expected result is 2.35

I'm getting a build error.

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

Upvotes: 0

Views: 2875

Answers (6)

Val Nolav
Val Nolav

Reputation: 902

double? AssessmentTotal=2.345;
double? AssessmentTotalval = Math.Round((decimal)AssessmentTotal == null ? 0 
 : AssessmentTotal.Value, 2);
Console.WriteLine(AssessmentTotalval);

** Correction **

The code above is throwing error if the value is null. The code below is working better

double? AssessmentTotalval = AssessmentTotal != null ? Math.Round(AssessmentTotal.Value, 2) : 0;

Upvotes: 0

xanatos
xanatos

Reputation: 111850

Considering that rounding is a non-inexpensive operation, you could:

double? AssessmentTotalval = AssessmentTotal == null ? 
                    0.0 : 
                    Math.Round(AssessmentTotal.Value, 2, MidpointRounding.AwayFromZero)

and bypass totally the Math.Round if the AssessmentTotal is null.

Note that at this point AssessmentTotalval is always != null, so it is useless to have a double?

Note even the use of MidpointRounding.AwayFromZero, otherwise 0.005 would become 0 instead of 0.01.

Upvotes: 4

ANewGuyInTown
ANewGuyInTown

Reputation: 6437

you are missing 'm'. It should be 0.0m in your condition. 0 by default is int in most cases and 0.0 by default is double.

edit

Taking a closer look at it , looks like you are just converting double to decimal while comparing with null only , result is still double or implicit conversion of int 0 to double. However I suggest you modify your result to be decimal and do the rounding like in the formula I provided below. I will explain the reason later when I am at PC, if you want. But right now I am typing from my iPhone 5 and it's darn difficult .:(

While you are at it, you should also take a look at rounding it with mid- point rounding. Math.Round((Decimal)d, 3, MidpointRounding.AwayFromZero))

Upvotes: -1

Sandeep
Sandeep

Reputation: 1210

double? AssessmentTotalval = Convert.ToDouble((AssessmentTotal ?? 0).ToString("N2"));

Upvotes: -2

sudhansu63
sudhansu63

Reputation: 6180

You can do it like below.

double? AssessmentTotal=2.345;
double? AssessmentTotalval = AssessmentTotal.HasValue ? Math.Round(AssessmentTotal.Value,2) : (double?)null;

If you want 0 when AssessmentTotal is null then you can modify the above code as below.

double? AssessmentTotal=2.345;
double? AssessmentTotalval = AssessmentTotal.HasValue ? Math.Round(AssessmentTotal.Value,2) : (double?)0;

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117057

Try doing it like this:

double? AssessmentTotal = 2.345;
double? AssessmentTotalval = Math.Round(AssessmentTotal.GetValueOrDefault(), 2);

There are a number of issues with your current approach.

First, the condition (decimal)AssessmentTotal == null only evaluates to true if AssessmentTotal is not null. As soon as it is null you get a "InvalidOperationException: Nullabl object must have a value." exception. Calling AssessmentTotal.GetValueOrDefault() avoids that issue.

Second, Math.Round(...) takes either a double or decimal - by using 0 you make your code less readable (and possibly buggy). You should always use either 0.0 for double or 0m for decimal. In your case you should use 0.0 since you are dealing with a double.


As per your comment re rounding up 49.625 you need to change the code to this:

double? AssessmentTotal = 49.625;
double? AssessmentTotalval = Math.Round(AssessmentTotal.GetValueOrDefault(), 2, MidpointRounding.AwayFromZero);

The MidpointRounding default is MidpointRounding.ToEven - you want MidpointRounding.AwayFromZero.

Upvotes: 2

Related Questions