We help IT Professionals succeed at work.

# C# - reading 2 digits to right of decimal on type double var

on
Medium Priority
903 Views
Anyone know a simple way to write code for reading the 2 digits to the right of decimal of a variable of type double..  For instance, let's say we want to accept all numbers, i.e...

54.28
54.31
54.88
55.02

but we don't want to accept any numbers that have the 2 digits to the right of the decimal ==     .00   or    .50      as follows...

54.00
54.50
55.00
55.50

etc...

so in pseudocode..   it may look something like this..

If ( last 2 digits == .50  ||  last 2 digits == .00 )
{
return;
}

Lot of points here for the right answer!  Thanks all.

Comment
Watch Question

## View Solutions Only

Commented:
int i = (int)(yourdouble * 100) % 100;
if (i == 50 || i == 0)
{
return;
}

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
hi ,
double[] dds = {54.28, 54.31, 54.88, 55.02, 55.60, 55.30, 59.60};
for(int cnt = 0; cnt < dds.Length; cnt++)
{
if((int)Math.Ceiling(dds[cnt]) != (int)Math.Round(dds[cnt]))
{
MessageBox.Show("ding ding ding");
}
}

regards,
satish.

Commented:
Thanks to both!
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile