 Check if a decimal number is halfway from two integers c# [Answered]RSS

9 replies

Last post Jun 29, 2017 06:59 AM by Eric Du

• 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....

Thanks

• 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?

Mark as answer posts that helped you. • Re: Check if a decimal number is halfway from two integers c#

Jun 28, 2017 07:41 PM|xtinction84|LINK

In a web application..

• 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,

Mark as answer posts that helped you. • 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

• 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

decimal x  = 4.3M;
decimal roundedx = Math.Round(x*2, MidpointRounding.AwayFromZero)/2;
Console.WriteLine(roundedx);

//results in 4.5

Mike Banavige
• 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!

• Re: Check if a decimal number is halfway from two integers c#

Jun 29, 2017 12:42 AM|mbanavige|LINK

xtinction84

if 4.3 should be 4.0 or just 4

xtinction84

I dont want to round

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

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...

Mike Banavige
• 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:

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.

• 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:

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.