Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hello, I have a question. I have to round number by 0.5.
for example,
15.234 should be 15
15.617 should be 15.5
15. 256 should be 15.5
15.25 should be 15.5
15.880 should be 16
15.7777 should be 16
So, I look in which 1/4 my fractional part is presented. If i have a number in the middle point i have to round up, but I can have fractional parts from 3, 4 and more digits.

thanks
Posted
Comments
Sergey Alexandrovich Kryukov 27-May-15 18:13pm    
What is "math.rule"? :-)
—SA
Yvan Rodrigues 28-May-15 15:00pm    
[Removed from solution]

Sergey and Yvan, you are right, I want to round up to nearest 0.5 and it can be up or down.
Yvan, so peaty that your code is not on vb.net, but thank you very much.

The simplest solution would probably be:
VB.NET
Function Round(ByVal value As Double) As Double
    Return Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2
End Function

Dim values() As Double = { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 }
 
For Each v As Double In values
    Console.WriteLine(Round(v))
Next

' Output:
' 15
' 15.5
' 15.5
' 15.5
' 16
' 16
 
Share this answer
 
Comments
CPallini 28-May-15 15:52pm    
5.
Are you just saying that you want to round up all those numbers "to nearest 0.5"? No, this is not how standard Math methods work. You have to develop your own rounding method. (By the way, why these rounding rules?)

So, you are going to develop appropriate arithmetic yourself. I can give you one advice which could allow you to use standard Math.Round at the core of your own method. This is the idea: do the coordinate transform: divide all you numbers by 0.5 (that is, multiply by 2) and do all your intermediate calculations in that "double-scaled" coordinate system. In other words, do all the calculations in the where the unit is 0.5, and all numbers are "numbers of 0.5 units". Then the rounding will be reduced to the rounding to nearest 1. At the very end, when you present the results, you can get back to original system my multiplying the results by 0.5.

Alternatively, you can do all the calculations by yourself, starting from separating fractional and integer part of the number. In turn, you can do it by yourself, or you can use the methods
Math.Floor and/or Math.Ceiling. In other words, all you need is a bit of logical and mathematical thinking.

—SA
 
Share this answer
 
Comments
Nictarine 27-May-15 22:29pm    
Sergey, thanks a lot for your description.Probably, to use russian for us can be better.))) Actually, I mean mathematical rule under the "math.rule". Execuse me about this not clear explaination.
Sergey Alexandrovich Kryukov 27-May-15 23:22pm    
You are welcome. Will you accept the answer formally?

Sorry, I never heard of such member as Math.rule. What is that? Any links?

About Russian: I really love the language and use it expertly, but the only problem is that other people should be able to understand all we right here. I even used it in some cases on this site, but in this case I translate and explain everything I write... :-)
If you want to clarify something in Russian, I think you can do it, but please double it in English; if your translation is not 100% adequate, I'll try to help to fix it...

—SA
CPallini 28-May-15 15:50pm    
5.
Sergey Alexandrovich Kryukov 28-May-15 16:14pm    
Thank you, Carlo.
—SA
I understand you want to round to the nearest 0.5 without using System.Math.
C#
double[] values = new[] { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };

foreach (double v in values)
    Console.WriteLine("{0} rounds to {1}", v, Round(v));

double Round(double value)
{
    return ((int)((value+0.251)*2))/2.0;   
}

Outputs:
15.234 rounds to 15
15.617 rounds to 15.5
15.256 rounds to 15.5
15.25 rounds to 15.5
15.88 rounds to 16
15.7777 rounds to 16

I believe in VB.NET, it would be something like:
VB
Dim values() As Double = { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };

For Each v As Double In values
    Console.WriteLine(Round(v))
Next

Function Round(ByVal value As Double) As Double
    Round = CInt(((value+0.251)*2))/2.0f
End Function
 
Share this answer
 
v2
Comments
CPallini 28-May-15 15:52pm    
why .251 (as opposed to .25)?
Yvan Rodrigues 28-May-15 16:05pm    
It's an exxageration (to make a point) of adding floating point bias. All floating point types like float/single and double are approximate. The value 5 could actually equal 5.000002323 or 4.999999634. If the slightly lower than the desired perceived value, a rounding function could rounded to an undesired lower value during the cast to int. The bias corrects this. It would actually be more like 0.25001, depending on how much precision we want in the rounded value.

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