Formatting Math.Pow Output

I hate to ask such a stupid question, but I'm just getting started, so here it goes.

myString = "2 to 2.5 power is " + Math.Pow(2, 2.5);

I want to format the total number to 4 decimal places and show the string in a MessageBox. I can not understand this or find the answer in the book. Thank!

+3
source share
5 answers

The ToString method should do the trick. You may need to search it on MSDN to find additional formatting options.

Math.Pow(2, 2.5).ToString("N4")
+4
source

a string MessageBox, MessageBox.Show. , overload, string, MessageBox. ,

string s = // our formatted string
MessageBox.Show(s);

, string. String.Format. Standard Numeric Format Strings MSDN. , "F" "F":

( "F" ) "-ddd.ddd...", "d" (0-9). "" .

.

,

double result = Math.Pow(2, 2.5);
string s = String.Format("2 to 2.5 power is {0:F4}", result);

, ,

double result = Math.Pow(2, 2.5);
string s = String.Format("2 to 2.5 power is {0:F4}", result);
MessageBox.Show(s);
+3
string.format("2 to 2.5 power is {0:0.000}", Math.Pow(2, 2.5));
+2
source
Math.Pow(2, 2.5).ToString("N4") 

- this is what you want, I think.

advanced formatting options

+1
source

This is not a stupid question: some of the other answers are incorrect.

MessageBox.Show(string.Format("2 to 2.5 power is {0:F4}", Math.Pow(2, 2.5)));
+1
source

Source: https://habr.com/ru/post/1731686/


All Articles