0

I'm making a calculator. I've got a textbox which shows the output calculations (txtdisplay.Text). But it only fits 8 characters so if I have a calculation with more than 8 characters it will be cut off and cannot be seen.

How do i make my result (e.g 120000000) into (1.2e+10) whenever the amount of characters reaches over 8?

Note that it's actually two questions, how do I make something happen when a textbox reaches 9 characters and how do I subsequently convert the content of the textbox to a scientific notation?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Daniel
  • 9
  • 1
    So what if I type `1234567891`? It would be `1.234567891e+9`, which that output is more than 9 characters. – gunr2171 Sep 29 '14 at 16:07
  • Also why would you limit your textbox to only 8 characters? – gunr2171 Sep 29 '14 at 16:10
  • @gunr2171 fair point i noticed halfway through writing that that would be an issue as wel, guess i have to find a way to round off? but i'm doing one issue at a time. It's limited to 8 characters because i've got my font set to 48 and want it to be a basic looking calculator (like the windows one) and not a calculator with a 2 page long result. Or is this not what you meant? – Daniel Sep 29 '14 at 17:21

4 Answers4

2

You can use a format specifier when converting the double to a string. The "G" format specifier uses the shorter of fixed-point and scientific notation. You can specify a maximum number of digits for the cutoff using "G#". So, for example to limit the number to 8 digits use:

double x = 120000000;
string text  = x.ToString("G8"); // 1.2E+08

If you really want a limited number of characters you could do something like this:

private static string FormatDouble(double d)
{
    string text = d.ToString();
    return text.Length > 8 ? d.ToString("0.#E+0") : text;
}

Note that 8 characters is extremely limited. You've got to display the mantissa's sign, exponent (up to 3 characters), exponent's sign, decimal separator, "E", which can be up to 7 characters without even considering the mantissa itself.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Your code outputs `12000000`. Although the input example in the question works with this code (you are missing one 0). – gunr2171 Sep 29 '14 at 16:13
  • @gunr2171 It all depends on the number of digits you want. I think the OP wants more than 2 digits though, since it says over 8. I changed the input since that was exactly 8 which would show in fixed-point. – Mike Zboray Sep 29 '14 at 16:15
  • Great piece of information and a clean way of doing it, but i'm afraid can't implement it as i have a greatly fluctuating double and it has to be done when the text reaches over 8 characters which is best to be done in the text.changed handler i believe. – Daniel Sep 29 '14 at 17:16
  • @Daniel I posted an alternative, maybe you can use that. I don't see how TextChanged is going to be used here. You should formatting the number before putting it into the text box. – Mike Zboray Sep 29 '14 at 17:42
  • @mikez Thanks for the input and i understand the code i think, or atleast the main idea. But when i try and implement it myself i try this : double output = FormatDouble(result); where result is a double. and it gives me the error can't convert string to double. Now when i add this code to convert the result to a string like this : string testresult = Convert.ToString(result); and then do the same. double output = FormatDouble(testresult); it gives me two errors, now it says can't convert string to double and the best overload method for (formatdouble) has some invalid arguments – Daniel Sep 29 '14 at 20:26
0

This code converts you number to scintific notation:

yournumber.ToString("G2", CultureInfo.InvariantCulture)

This code can help you to determine that text was changed:

textBox.TextChanged += new EventHandler(textBox_TextChanged);

...    

private void textBox_TextChanged(object sender, EventArgs e)
{
//code to handle changes
}
Anton
  • 9,682
  • 11
  • 38
  • 68
  • Not quiet sure what's going on here but that's probably my own incompetence. Visual studio 2013 doesn't seem to recognize invariantculture nor cultureinfo and where would i put the += new eventhandler? – Daniel Sep 29 '14 at 17:18
  • @Daniel you should add using System.Globalization; to use CultureInfo.InvariantCulture – Anton Sep 29 '14 at 17:25
  • @Daniel adding event handlers could be put to your form constructor. – Anton Sep 29 '14 at 17:31
0
 private void CalcTextBox_TextChanged(object sender, RoutedEventArgs e)
 {
     int testInt = 0;

     if (((TextBox)sender).Text.Length > 8 && int.TryParse(((TextBox)sender).Text, out testInt))
     {
        ((TextBox)sender).Text = String.Format("{0:E2}", testInt); 
     }
 }

Attach to the TextChanged Event, and set it there.

Chuck Buford
  • 321
  • 1
  • 9
  • i corrected the tryparse in the if ( should be , **out** testInt) but even with that corrected and no errors showing in the code, when i run it and reach over 8 characters it gives an error at the line ((textbox)sender).text = string.format("{0:E2}"); saying that the index (zero based) must be greater than or equal to zero and less than the size of the argument list. honestly not a clue what this could be reffering to – Daniel Sep 29 '14 at 17:10
  • fixed the out. What text are you entering? I just tested this, and it works fine. – Chuck Buford Sep 29 '14 at 17:55
  • @Daniel can you post what text you are entering? I just tested this, and it works fine. – Chuck Buford Sep 29 '14 at 18:32
  • I just press a number (Eg 5) on my numpad of the calculator 9 times, so 5 shows up and then another till it ends up with 9. The second it does it shows 555555555 and it gives the error – Daniel Sep 29 '14 at 19:51
  • @Daniel What is the value of the integer being passed into the STring.Format function? – Chuck Buford Sep 29 '14 at 22:04
  • Does it not tryparse textbox.text into the testint? if so the value would be 5555555555 even though any value that i put in gives the error – Daniel Sep 30 '14 at 18:04
  • @Daniel It should be, but I suspect something is going wrong with it in your code. Can you debug and confirm that testInt does have a value at that line? – Chuck Buford Sep 30 '14 at 19:15
0

Double.ToString() provides a level of this capability out of the box. The default capability doesn't seem to suit your particular issue. You could use a specific formatting such as Double.ToString("G8", CultureInfo.InvariantCulture), but this may also be insuffient. You may need to create your own implementation of IFormatProvider to satisfy your needs.

Please consider the following resources:

http://msdn.microsoft.com/en-us/library/shxtf045.aspx

When does Double.ToString() return a value in scientific notation?

Community
  • 1
  • 1
Aaron Palmer
  • 8,912
  • 9
  • 48
  • 77