Avoid Scientific Notation RRS feed

  • Question

  • Sometimes I get scientific notation as an answer to a math operation in a text box.

    I need to avoid this and also do not need an answer with more than 8 or 10 decimal places.

    For example, lets say the answer is 10.01234567890123 then an answer of 10.0123456789 is desired.

    How might I do this?

    My present code:

    double a;

    double b;

    double ans;

    ans = a / b;

    answerTextBox.Text = ans.ToString();

    Tuesday, June 2, 2020 4:20 PM

All replies

  • When converting to a string use the F format specifier to forced fixed point notation.  Precision is controlled by the current culture's settings. If you want more precision (and it is available then use a number after the F (e.g. F8)

    answerTextBox.Text = ans.ToString("F");

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 2, 2020 4:40 PM
  • Thanks for the reply.

    Adding the parameter "F8" does seem to do the trick but it also seems to affect the text box I am typing in and not just the answer text box. I am doing updates to the answer text box as I type.

    • Edited by jack0987 Tuesday, June 2, 2020 7:40 PM
    Tuesday, June 2, 2020 7:38 PM
  • Not sure what you mean by that. ToString() simply returns back the current value as a string. The Text property on the box just sets the text shown in the textbox. They are unrelated otherwise. No value of Text will impact the formatting of the box itself, just its content. You can confirm this by doing this.

    var ans = 4.56;
    //Identical 1 (4.56000000)
    answerTextBox.Text = ans.ToString("F8");
    //Identical 2 (4.56000000)
    var str = ans.ToString("F8");
    answerTextBox.Text = str;
    //Identical 3
    answerTextBox.Text = "4.56000000";
    //Identical 4 but without the padded zeroes
    answerTextBox.Text = ans.ToString("F");

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 2, 2020 7:44 PM
  • Here is a more complete code:

    Please see what you make of it. Need to do more work on it as it needs checking for non-numeric entries, etc.

    private void AMTUSDTextBox_TextChanged(object sender, EventArgs e)
                double AmtUSDAns = 0;
                double MarketUSDAns = 4.00; //set to anything just so it will not be zero
                double ans;

                if (AMTUSDTextBox.Text.FirstOrDefault() == '.')
                    AMTUSDTextBox.Text = "0" + AMTUSDTextBox.Text;
                    AMTUSDTextBox.Select(AMTUSDTextBox.Text.Length, 0);
                if (AMTUSDTextBox.Text == "0" || AMTUSDTextBox.Text == "")
                { return; }
                { AmtUSDAns = double.Parse(AMTUSDTextBox.Text); }
                if (MarketUSDTextBox.Text == "0" || MarketUSDTextBox.Text == "")
                { return; }
                { MarketUSDAns = double.Parse(MarketUSDTextBox.Text); }

                ans = AmtUSDAns / MarketUSDAns;
                AMTTextBox.Text = ans.ToString();            

    Tuesday, June 2, 2020 8:00 PM
  • So you have a textbox where the user enters a USD amount. You have a separate textbox that accepts "market USD". Whenever the user enters a character into the USD amount you're going to update the AMTTextBox with the amt / market.

    The code starts by looking to see if the amount starts with a dot. Not really sure why you need this but this is not the best way to solve that. Nevertheless it prepends a 0 in front. Then it selects the entire value.

    Next it checks for an empty string or 0. If it finds either then it aborts otherwise it parses the string. However if the user enters something that isn't a double value then it will blow up. Same thing for the market USD.


    1) Use Double.TryParse to parse both textbox values and store each result into a double value. If parsing of either fails or either value is 0 then abort (although this won't reset your UI either which also seems wrong to me).

    2) Do the division and insert into the AMT box.

    3) Local variables should be camel cased. Refer Pascal casing for type members.

    Something like this (not tested).

    //Helper method, using -1 for an invalid value
    private double GetDecimalValue ( Control control )
       if (Double.TryParse(control.Text, out double result))
          return result;
       return -1;
    private void AMTUSDTextBox_TextChanged(object sender, EventArgs e)
       //Validate first
       double amtUsd = GetDecimalValue(AMTUSDTextBox.Text);
       double marketUsd = GetDecimalValue(MarketUSDAns.Text);
       if (amtUsd <= 0 || marketUsd <= 0)
       double ans = amtUsd / marketUsd;
       AMTTextBox.Text = ans.ToString("F");            

    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Naomi N Wednesday, June 3, 2020 3:10 PM
    Tuesday, June 2, 2020 9:56 PM
  • Hi jack0987,
    As CoolDadTx said, Double.TryParse method is a better choice. This method indicates whether the conversion succeeded or failed.
    And I made a test by changing this line:

    AMTTextBox.Text = ans.ToString();   


    AMTTextBox.Text = ans.ToString("F10"); 

    It works fine as shown below:
    So what problems do you encounter?
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 3, 2020 3:00 AM
  • Info: Working on a Windows XP Pro computer with VC2010

    (Should update it one of these days.)

    The TryParse is a better way. Thanks.

    I am still working on the code but did revise the helper method to this:

    //Helper method, using -1 for an invalid value
            private double GetDecimalValue(string text)
                double result;
                if (double.TryParse(text, out result))
                    return result;
                return -1;

    I will also change the return value to a boolean to allow for negative numbers.

    For some reason, the "F10" parameter affects the text box I am typing in the same way as the answer text box. I can overcome this in code by saving it's value and then restoring it after the answer text box has been set.

    Wednesday, June 3, 2020 7:41 AM
  • "For some reason, the "F10" parameter affects the text box I am typing in the same way as the answer text box"

    Again, not possible as you are only setting the Text property of a textbox. Please provide an exact description (with steps) and/or screenshot of the "affect" that it is having. Also, you have 3 textboxes in your code. Be precise about what textbox you are talking about. Verify you don't have another event handler reacting to the changing of the answer text box that is actually causing the issue. A breakpoint would help with this.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 3, 2020 1:04 PM