Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have attempted this question and I need a tiny bit of help with the last part. I'm constantly doing research, as I'm new to this, but help from people is beneficial.
Please see what I have done so far below.

The problem is, it is saying that "the call is ambiguous between the following methods or properties: 'Math.Round(double, int)' and 'Math.Round(decimal, int)'.

As far as I am aware, I need to convert the variable into a decimal or a double, and then round this to 2dp, but I am having issues doing this successfully (i.e. I know what I want to do, but as far as coding it correctly without errors, I have not been able to do that as of yet).

Regards.

What I have tried:

public static double HowManyHoursFromNow(DateTime deadline)
{
    var remaining = deadline.Subtract(DateTime.Now).Hours;

    return Math.Round(remaining, 2);
}
Posted
Updated 13-Jul-17 0:46am
v2

Timespan has several properties. You can get the Days/Hours and Minutes components but:

The Hours will never go over 23. this is handled in the Days property. It will never be more precise than and int. this is handled by the Minute property
The minutes will never go over 59. etc...

What you're looking for is the TotalHours. This can be over 23 and is as precise as a double can be.
 
Share this answer
 
Comments
Member 13302374 13-Jul-17 6:39am    
Thank you Sir!
Member 13302374 13-Jul-17 7:16am    
So, let's say for example, I had a question like: Given the date and time of an appointment return how many days it is in the future (in full days e.g. at 11pm tonight, an appointment at 9am tomorrow is 1 day).

My code would be:
public static int HowManyDaysFromToday(DateTime appointment)
{return appointment.TimeOfDay.Subtract(DateTime.Now.TimeOfDay).TotalDays;}

Is this correct?
Andy Lanng 13-Jul-17 7:28am    
No. The issue here is that a timespan has no reference to when it starts or finishes. between 11pm and 9am there are 10 hours. 10 hours is the only detail stored in a timespan. It's the exact same object as 11am to 9pm the same day

You would need to compare the dates as a part of the equation. For I full answer I'll need you to post a new question :S
Member 13302374 13-Jul-17 7:33am    
I've posted a new question :)
Andy Lanng 13-Jul-17 7:52am    
Oh, I don't see it ?
Can you post the link plz ^_^
The DateTime.Subtract method returns a TimeSpan object, and the Hours property of that is an int. So Math.Round cannot do anything with it as it has no fractional part; you can only round a Decimal or Double type as described in Math.Round Method (System)[^].
 
Share this answer
 
You should use the TotalHours property which represents whole and fractional hours.
public static double HowManyHoursFromNow(DateTime deadline)
{
    var remaining = deadline.Subtract(DateTime.Now).TotalHours;

    return Math.Round(remaining, MidpointRounding.AwayFromZero);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900