Solved

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

Posted on 2006-11-03
3
868 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to both!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now