I have two tables
Video
Id,
VideoName,
VideoUrl
Ratings
Id,
VideoId,
CommentDesc,
Rating (decimal type)
Multiple users can submit ratings for 1 video multiple times. Each individual’s rating is stored in the rating column and can submit a rating including halves i.e. 1.5, 2.5
On my webpage I have a drop-down where the user can select the average rating (the values are 1-5) and will display all the videos with that average rating.
I can get the average using Linq to SQL using something like
return myDataCtx.Ratings.Average(i => i.Rating));
But if a user selects a whole number between 1-5 from a drop-down how could I round the rating column to the nearest whole number?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can get the nearest number do like this.
decimal d = 1.75M; var ceiling = Math.Ceiling(d); var floor = Math.Floor(d); var closest = ceiling - d < d - floor ? ceiling : floor;
x.5 should have to be taken care of separately .
You can also use Math.Round()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0