To show a string
in a MessageBox
you use MessageBox.Show
. In particular, there is an overload accepting a single string
parameter that will be displayed in the MessageBox
. Thus, we need
string s = // our formatted string
MessageBox.Show(s);
Now, let's figure out what our string
is. A useful method here is String.Format
. A useful reference here is the Standard Numeric Format Strings page on MSDN. In particular, I draw your attention to the fixed-point specifier "F"
or "f"
:
The fixed-point ("F) format specifier converts a number to a string of the form "-ddd.ddd…" where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative.
The precision specifier indicates the desired number of decimal places.
Thus, we want
double result = Math.Pow(2, 2.5);
string s = String.Format("2 to 2.5 power is {0:F4}", result);
so, putting it all together,
double result = Math.Pow(2, 2.5);
string s = String.Format("2 to 2.5 power is {0:F4}", result);
MessageBox.Show(s);