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
*/
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
multipleof 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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
56 Points
142 Posts
Check if a decimal number is halfway from two integers c#
Jun 28, 2017 07:23 PM|xtinction84|LINK
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
All-Star
35218 Points
9955 Posts
Moderator
Re: Check if a decimal number is halfway from two integers c#
Jun 28, 2017 07:40 PM|bbcompent1|LINK
Are you doing this in a web application or desktop/console app?
Member
56 Points
142 Posts
Re: Check if a decimal number is halfway from two integers c#
Jun 28, 2017 07:41 PM|xtinction84|LINK
In a web application..
All-Star
35218 Points
9955 Posts
Moderator
Re: Check if a decimal number is halfway from two integers c#
Jun 28, 2017 07:43 PM|bbcompent1|LINK
Ok, well post the code you have and we'll try to get you unstuck,
Member
56 Points
142 Posts
Re: Check if a decimal number is halfway from two integers c#
Jun 28, 2017 07:44 PM|xtinction84|LINK
I m implementing a stars rating application.
So I dont want to round for numbers like 0.5 ; 1.5 ; 2.5
All-Star
160051 Points
13198 Posts
ASPInsiders
Moderator
Re: Check if a decimal number is halfway from two integers c#
Jun 28, 2017 11:28 PM|mbanavige|LINK
to round to the nearest 1/2, first multiple your number by two, then round, then divide by two
Member
56 Points
142 Posts
Re: Check if a decimal number is halfway from two integers c#
Jun 29, 2017 12:33 AM|xtinction84|LINK
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!
All-Star
160051 Points
13198 Posts
ASPInsiders
Moderator
Re: Check if a decimal number is halfway from two integers c#
Jun 29, 2017 12:42 AM|mbanavige|LINK
your 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
4.3 becomes 4
4.5 stays 4.5
4.6 becomes 4.5
etc...
Member
56 Points
142 Posts
Re: Check if a decimal number is halfway from two integers c#
Jun 29, 2017 12:59 AM|xtinction84|LINK
No. I think I find the solution... like this:
I dont know if there is a solution more fast.
Contributor
6730 Points
2715 Posts
Re: Check if a decimal number is halfway from two integers c#
Jun 29, 2017 06:59 AM|Eric Du|LINK
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:
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.