Answered by:
Check if a decimal number is halfway from two integers c#
Question

User30110692 posted
I need to round the numbers like this:
If 1 or 1.5 or 2 or 2.5 or 3 or 3.5 or 4 or 4.5 or .... and continues....
does not round!
if for example 1.1 rounds to 1,  Math.Round(1.1, 0) = 1
if for example 1.7 rounds to 2  Math.Round(1.7, 0) = 2
My problem is do deal with the numbers like 0.5, 1.5, 2.5 and so on....
Please help me.
Thanks
Wednesday, June 28, 2017 7:23 PM
Answers

User1838255255 posted
Hi xtinction84,
According to your description, as far as I know, as far as I know, you could use division to check if this value is a multiple of 0.5.
Sample Code:
double a = Convert.ToDouble(TextBox1.Text); if (a % 0.5 == 0) { Response.Write(a); } else { Response.Write(Math.Round(a)); }
Best Regards,
Eric Du
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 29, 2017 6:59 AM
All replies

User718146471 posted
Are you doing this in a web application or desktop/console app?
Wednesday, June 28, 2017 7:40 PM 
User30110692 posted
In a web application..
Wednesday, June 28, 2017 7:41 PM 
User718146471 posted
Ok, well post the code you have and we'll try to get you unstuck,
Wednesday, June 28, 2017 7:43 PM 
User30110692 posted
I m implementing a stars rating application.
So I dont want to round for numbers like 0.5 ; 1.5 ; 2.5
Wednesday, June 28, 2017 7:44 PM 
User158764254 posted
to round to the nearest 1/2, first multiple your number by two, then round, then divide by two
decimal x = 4.3M; decimal roundedx = Math.Round(x*2, MidpointRounding.AwayFromZero)/2; Console.WriteLine(roundedx); //results in 4.5
Wednesday, June 28, 2017 11:28 PM 
User30110692 posted
No.
if 4.3 should be 4.0 or just 4
in case 4.5, should not round.
My question is, how I detect that any number (like 4.5) is half from 4 and half from 5, so, I dont want to round!
Thursday, June 29, 2017 12:33 AM 
User158764254 posted
if 4.3 should be 4.0 or just 4I dont want to roundyour above two statements contradict each other.
if 4.3 should become 4, then you just rounded down.
If you'd prefer to round down, then you could do this
decimal x = 4.3M; decimal roundedx = Math.Floor(x*2)/2; Console.WriteLine(roundedx);
4.3 becomes 4
4.5 stays 4.5
4.6 becomes 4.5
etc...
Thursday, June 29, 2017 12:42 AM 
User30110692 posted
No. I think I find the solution... like this:
var value = 4.3; var max = Math.Ceiling(value); var min = Math.Floor(value); var ret = 0.0; var isHalf = ((max + min) / 2 == value) if (isHalf) //Is half or int, so, not round ret = value; else ret = Math.Round(value, 0); //round Console.WriteLine(ret); /* So: 4.3  goes to 4.0 4.7  goes to 5.0 4.5  stay igual, so 4.5 0.1  goes to 0.0 0.6  goes to 1.0 0.5  stay igual, so 0.5 */
I dont know if there is a solution more fast.
Thursday, June 29, 2017 12:59 AM 
User1838255255 posted
Hi xtinction84,
According to your description, as far as I know, as far as I know, you could use division to check if this value is a multiple of 0.5.
Sample Code:
double a = Convert.ToDouble(TextBox1.Text); if (a % 0.5 == 0) { Response.Write(a); } else { Response.Write(Math.Round(a)); }
Best Regards,
Eric Du
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 29, 2017 6:59 AM