Reputation: 1192
Hi all i've searched the web but have been able to come up with a solution to this issue. Basically i want to round a number of days to the nearest year. So if i have a value of 250 days, this should round up to 1 year or if i had 400 days this would round up to 2 years. Any ideas?
Upvotes: 1
Views: 7919
Reputation: 1502116
Years vary in length - should 366 days be one year or two?
Assuming a 365-day year, you would want something like:
int years = (days + 364) / 365;
... that ensures that an exact number of years doesn't round at all, but anything else rounds up.
Another alternative would be:
int years = (int) Math.Ceiling(days / 365.0);
Upvotes: 6
Reputation: 8417
Well, now that Jon Skeet commented I obviously don't stand a chance... :(
As noted in comments below, his solution is the one that should be looked at. If anyone was doubting this...
int days = 400;
int roundedYears = (days / 365) + 1
Upvotes: 0