Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

mystery 2.3

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vince13 - 11 Jul 2007 20:19 GMT
I am working on a page with textboxes that can only accept a certain number
of decimals places.  So I wrote a small function to check the number of
decimals, not counting zeros, as well as range check the entry and then
return the results, based on a parsing method I learned in c++ involving
integer division:

public bool entryCheck(int decimals, double min, double max, TextBox field,
Label error)
{
               bool valid = false;
               if (field.Text != "")
    {
        error.Text = "";
        double entry = double.Parse(field.Text);
        //decimal check
        if ((double)((int)(entry * Math.Pow(10,(decimals + 1))) / 10) != entry *
Math.Pow(10, decimals))
        {
            field.ForeColor = Color.Red;
            error.Text = "Too Many Decimals";
        }
        //range check
        else if (entry < min || entry > max)
        {
            field.ForeColor = Color.Red;
            error.Text = "Invalid Range";
        }
        else
        {
            field.ForeColor = Color.Blue;
            valid = true;
        }
    }
    else
        error.Text = "Required Field"; //error if left blank
           
    return valid;
}

It works beautifully ALMOST all of the time.  For some reason, when the entry
is 2.3, the computer multiplies it by say 10000 and gets 22999.9999, which
obviously won't work and isn't correct.  Does anyone know why this is?
Thanks?
bruce barker - 11 Jul 2007 20:46 GMT
because floating point is done in base 2, not base 10. just as 1/3 is
.33333... in base 10, 2.3 is a repeating number in base 2

switch to the decimal datatype which uses base 10 (actual its integer
only with a implied decimal).

-- bruce (sqlwork.com)

> I am working on a page with textboxes that can only accept a certain number
> of decimals places.  So I wrote a small function to check the number of
[quoted text clipped - 39 lines]
> obviously won't work and isn't correct.  Does anyone know why this is?
> Thanks?
John Timney (MVP) - 11 Jul 2007 22:29 GMT
I prefer this rather simplistic approach

Double start_value = 5231.327000;
string[] end_values = start_value.ToString().Split('.');
int fubar = (end_values.Length > 1) ? end_values[1].Length : 0;
Response.Write(fubar.ToString());

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog

> because floating point is done in base 2, not base 10. just as 1/3 is
> .33333... in base 10, 2.3 is a repeating number in base 2
[quoted text clipped - 51 lines]
>> obviously won't work and isn't correct.  Does anyone know why this is?
>> Thanks?
Vince13 - 12 Jul 2007 19:49 GMT
Thanks a lot guys!

**in case anyone else is wondering:

if ((((int)(dentry * (decimal)Math.Pow(10,(decimals + 1)))) / 10) != dentry *
(decimal)Math.Pow(10, decimals))

gave me the corrext result
Rad [Visual C# MVP] - 14 Jul 2007 21:25 GMT
>I am working on a page with textboxes that can only accept a certain number
>of decimals places.  So I wrote a small function to check the number of
[quoted text clipped - 39 lines]
>obviously won't work and isn't correct.  Does anyone know why this is?
>Thanks?

What I would suggest is to use a numeric up down control. This yields
a couple of benefits
- Less code
- Less headache
- Simpler to understand
- Intuitive to the users

--
http://bytes.thinkersroom.com

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.