tags:

views:

1339

answers:

6

I have to display ratings and for that i need increments as follows:

If the number is 1.0 it should be equal to 1
If the number is 1.1 should be equal to 1
If the number is 1.2 should be equal to 1
If the number is 1.3 should be equal to 1.5
If the number is 1.4 should be equal to 1.5
If the number is 1.5 should be equal to 1.5
If the number is 1.6 should be equal to 1.5
If the number is 1.7 should be equal to 1.5
If the number is 1.8 should be equal to 2.0
If the number is 1.9 should be equal to 2.0
If the number is 2.0 should be equal to 2.0
If the number is 2.1 should be equal to 2.0
and so on...

Is there a simple way to compute the required values?

+31  A: 

Multiply your rating by 2, then round using Math.Round(rating, MidpointRounding.AwayFromZero), then divide that value by 2.

John Rasch
+1 This suggestion is correct.
Eric J.
Works perfectly !!! Thanks :)
Murtaza RC
bah, if only i typed faster, lol
Neil N
@neil: http://www.amazon.com/Atari-25649-Typing-Tutor-Dummies/dp/B0001IQN9I :)
Jason
I dont need typing for dummies, I need typing for smarties
Neil N
Not perfect! what about integer overflow! You can compute only half of the possible integers.
Elazar Leibovich
@Elazar - if you could be ranked as high as 1,073,741,823rd, I can't think of a single use case where you would care if it's "one billion one and a half" or "one billion one" - if that's really a problem then there is something inherently flawed with the ranking scheme :)
John Rasch
+11  A: 

Multiply by 2, round, then divide by 2

if you want nearest quarter, multiply by 4, divide by 4, etc

Neil N
+1  A: 
decimal d = // your number..

decimal t = d - Math.Floor(d);
if(t >= 0.3d && t <= 0.7d)
{
    return Math.Floor(d) + 0.5d;
}
else if(t>0.7d)
    return Math.Ceil(d);
return Math.Floor(d);
Akash Kava
A: 

There are several options. If performance is a concern, test them to see which works fastest in a large loop.

double Adjust(double input)
{
    double whole = Math.Truncate(input);
    double remainder = input - whole;
    if (remainder < 0.3)
    {
        remainder = 0;
    }
    else if (remainder < 0.8)
    {
        remainder = 0.5;
    }
    else
    {
        remainder = 1;
    }
    return whole + remainder;
}
John Fisher
This should work, but it just isn't as elegant as some solutions given. Multiplying and using the system library is just sexy.
CaptnCraig
Performance is usually more important, and this could end up taking less time than the multiplication and division solutions.
John Fisher
This code is not correct. Since arithmetic with doubles usually has some small rounding errors, an operation such as 4.8 - 4.0 could give for example 0.799999... . In this case the code above would round to 4.5. Also better would to use Math.Floor instead of Math.Truncate, because right now negative numbers are not corretly rounded. I prefer the accepted answer,because it is simpler and less prone to implementation errors.
Accipitridae
+1  A: 

Sounds like you need to round to the nearest 0.5. I see no version of round in the C# API that does this (one version takes a number of decimal digits to round to, which isn't the same thing).

Assuming you only have to deal with integer numbers of tenths, it's sufficient to calculate round (num * 2) / 2. If you're using arbitrarily precise decimals, it gets trickier. Let's hope you don't.

Paul Brinkley
A: 

I had difficulty with this problem as well. I code mainly in Actionscript 3.0 which is base coding for the Adobe Flash Platform, but there are simularities in the Languages:

The solution I came up with is the following:

//Code for Rounding to the nearest 0.05
var r:Number = Math.random() * 10;  // NUMBER - Input Your Number here
var n:int = r * 10;   // INTEGER - Shift Decimal 2 places to right
var f:int = Math.round(r * 10 - n) * 5;// INTEGER - Test 1 or 0 then convert to 5
var d:Number = (n + (f / 10)) / 10; //  NUMBER - Re-assemble the number

trace("ORG No: " + r);
trace("NEW No: " + d);

Thats pretty much it. Note the use of 'Numbers' and 'Integers' and the way they are processed.

Good Luck!

Jason Henn