Solved

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

Posted on 2006-11-03
3
874 Views
Last Modified: 2006-11-20
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.

0
Comment
Question by:lblinc
3 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 400 total points
ID: 17867088
int i = (int)(yourdouble * 100) % 100;
if (i == 50 || i == 0)
{
return;
}
0
 
LVL 4

Assisted Solution

by:satish_nagdev
satish_nagdev earned 100 total points
ID: 17879286
hi ,
does following solves your problem
                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.
0
 

Author Comment

by:lblinc
ID: 17984970
Thanks to both!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question