mystery 2.3

Discussion in 'ASP .Net' started by Vince13 via DotNetMonster.com, Jul 11, 2007.

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

    --
    Message posted via http://www.dotnetmonster.com
    Vince13 via DotNetMonster.com, Jul 11, 2007
    #1
    1. Advertising

  2. Vince13 via DotNetMonster.com

    bruce barker Guest

    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)

    Vince13 via DotNetMonster.com wrote:
    > 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, Jul 11, 2007
    #2
    1. Advertising

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



    "bruce barker" <> wrote in message
    news:Obe1rS$...
    > 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)
    >
    > Vince13 via DotNetMonster.com wrote:
    >> 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?
    >>
    John Timney \(MVP\), Jul 11, 2007
    #3
  4. Vince13 via DotNetMonster.com, Jul 12, 2007
    #4
  5. On Wed, 11 Jul 2007 19:19:34 GMT, "Vince13 via DotNetMonster.com"
    <u35350@uwe> wrote:

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


    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
    Rad [Visual C# MVP], Jul 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    another array ranges mystery

    valentin tihomirov, Jun 18, 2005, in forum: VHDL
    Replies:
    2
    Views:
    478
    Mike Treseler
    Jun 18, 2005
  2. John K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,043
    John K. Humkey
    Jul 8, 2003
  3. bjh

    Mystery Repost

    bjh, Jan 13, 2004, in forum: Perl
    Replies:
    0
    Views:
    720
  4. Todd Plambeck

    Mystery Error

    Todd Plambeck, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    502
    Victor Garcia Aprea [MVP]
    Jul 15, 2003
  5. Jim Hammond

    datagrid template column binding mystery

    Jim Hammond, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    545
    Jim Hammond
    Nov 24, 2003
Loading...

Share This Page