Negative Numbers?

Discussion in 'ASP .Net' started by Charles A. Lackman, Sep 28, 2004.

  1. Hello,

    I have some textboxes that are doing addition:

    Dim AString As Double
    AString = Convert.ToDouble(TextBox1.Text) - Convert.To(DoubleTextBox2.Text)
    TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00")

    When the result is negative it is displayed as "(46)" or whatever the value
    is.
    This makes it impossible to test for a negative number to make further
    additions:

    TextBox5.text = Convert.ToDouble(TextBox3.text) +
    Convert.ToDouble(Textbox4.text)

    Is there a work-around for this? I must have negative numbers that can be
    added together.

    Thanks,
    Chuck
    Charles A. Lackman, Sep 28, 2004
    #1
    1. Advertising

  2. Don't save the number in the text box, save it in a variable. Just use the
    textbox for displaying the results.

    --
    Jonathan Allen


    "Charles A. Lackman" <> wrote in message
    news:e%...
    > Hello,
    >
    > I have some textboxes that are doing addition:
    >
    > Dim AString As Double
    > AString = Convert.ToDouble(TextBox1.Text) -

    Convert.To(DoubleTextBox2.Text)
    > TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00")
    >
    > When the result is negative it is displayed as "(46)" or whatever the

    value
    > is.
    > This makes it impossible to test for a negative number to make further
    > additions:
    >
    > TextBox5.text = Convert.ToDouble(TextBox3.text) +
    > Convert.ToDouble(Textbox4.text)
    >
    > Is there a work-around for this? I must have negative numbers that can

    be
    > added together.
    >
    > Thanks,
    > Chuck
    >
    >
    Jonathan Allen, Sep 28, 2004
    #2
    1. Advertising

  3. Charles A. Lackman

    Ben Lucas Guest

    It is displayed as "(46)" beacuse that is what your format specifier
    ("#,##0.00;(#,##0.00);0.00") is asking it to do for negative numbers.

    Since the Convert.ToDouble function does not recognize this format, you
    could check for the format you're looking for and change it appropriately:

    For example:

    string sText = TextBox3.Text
    if (sText.StartsWith("("))
    sText = "-" + sText.Remove("(").Remove(")")
    end if

    TextBox5.text = Convert.ToDouble(sText) + Convert.ToDouble(Textbox4.text)

    Or, probably the better way to do this is to just use the result in AString
    which is already a double and stores the value that you're looking for
    anyways.

    --
    Ben Lucas
    Lead Developer
    Solien Technology, Inc.
    www.solien.com



    "Charles A. Lackman" <> wrote in message
    news:e%...
    > Hello,
    >
    > I have some textboxes that are doing addition:
    >
    > Dim AString As Double
    > AString = Convert.ToDouble(TextBox1.Text) -

    Convert.To(DoubleTextBox2.Text)
    > TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00")
    >
    > When the result is negative it is displayed as "(46)" or whatever the

    value
    > is.
    > This makes it impossible to test for a negative number to make further
    > additions:
    >
    > TextBox5.text = Convert.ToDouble(TextBox3.text) +
    > Convert.ToDouble(Textbox4.text)
    >
    > Is there a work-around for this? I must have negative numbers that can

    be
    > added together.
    >
    > Thanks,
    > Chuck
    >
    >
    Ben Lucas, Sep 28, 2004
    #3
  4. Hi Charles,

    First of all, you're not doing addition; you're doing subtraction. Then,
    when you fill textbox5, you'r trying to convert a string to a double. Try
    debugging, set a few breakpoints, and follow the values.

    HTH,

    Bernie Yaeger

    "Charles A. Lackman" <> wrote in message
    news:e%...
    > Hello,
    >
    > I have some textboxes that are doing addition:
    >
    > Dim AString As Double
    > AString = Convert.ToDouble(TextBox1.Text) -

    Convert.To(DoubleTextBox2.Text)
    > TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00")
    >
    > When the result is negative it is displayed as "(46)" or whatever the

    value
    > is.
    > This makes it impossible to test for a negative number to make further
    > additions:
    >
    > TextBox5.text = Convert.ToDouble(TextBox3.text) +
    > Convert.ToDouble(Textbox4.text)
    >
    > Is there a work-around for this? I must have negative numbers that can

    be
    > added together.
    >
    > Thanks,
    > Chuck
    >
    >
    Bernie Yaeger, Sep 28, 2004
    #4
  5. Charles,
    In addition to the other comments.

    Use the Double.Parse overload (instead of Convert.ToDouble) that allows you
    to specify the number style to use when converting, something like:

    > AString = Double.Parse(TextBox1.Text, NumberStyles.Currency) -
    > Double.Parse(DoubleTextBox2.Text, NumberStyles.Currency)


    Read the help on NumberStyles enum to see what the various options allowed
    are.

    Hope this helps
    Jay


    "Charles A. Lackman" <> wrote in message
    news:e%...
    > Hello,
    >
    > I have some textboxes that are doing addition:
    >
    > Dim AString As Double
    > AString = Convert.ToDouble(TextBox1.Text) -
    > Convert.To(DoubleTextBox2.Text)
    > TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00")
    >
    > When the result is negative it is displayed as "(46)" or whatever the
    > value is.
    > This makes it impossible to test for a negative number to make further
    > additions:
    >
    > TextBox5.text = Convert.ToDouble(TextBox3.text) +
    > Convert.ToDouble(Textbox4.text)
    >
    > Is there a work-around for this? I must have negative numbers that can
    > be added together.
    >
    > Thanks,
    > Chuck
    >
    Jay B. Harlow [MVP - Outlook], Sep 28, 2004
    #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. prem_eda
    Replies:
    5
    Views:
    7,813
    Pieter Hulshoff
    Oct 11, 2004
  2. Mayank
    Replies:
    2
    Views:
    970
    Gunnar Hjalmarsson
    Nov 28, 2003
  3. Replies:
    3
    Views:
    12,501
    Boris Stumm
    Feb 9, 2006
  4. Matthew Wilson

    negative numbers and integer division

    Matthew Wilson, Oct 3, 2003, in forum: Python
    Replies:
    7
    Views:
    366
    mensanator
    Oct 3, 2003
  5. Tomi Silander
    Replies:
    3
    Views:
    878
    Grant Edwards
    Apr 6, 2005
Loading...

Share This Page