[SOLVED] Trying to restrict math.pow value upto 4 decimal value


I am trying to restrict value up to 4 decimal for Math.Pow((Density * 10), -12)

where double Density = 0; is defined as variable

now if Density=7;
then Math.Pow((Density * 10), -12) calculate as 7.22476158090089E-23
but I want it like 7.2247 E-8.

Any idea would be appreciated.


If you want to restrict the decimal precision to 4 decimal places you can use the Exponential Format Specifier, specifying the number of decimals to display "E4":

var answer = Math.Pow(70, -12);
var displayVal = string.Format("{0:E4}", answer);
// output

Note that

The exponent always consists of a plus or minus sign and a minimum of
three digits.

However, you can use the General Format Specifier which

If scientific notation is used … The exponent contains a minimum
of two digits
. This differs from the format for scientific notation
that is produced by the exponential format specifier, which includes a
minimum of three digits in the exponent.

You will need to use G5 to produce 5 significant digits:

var answer = Math.Pow(70, -12);
var displayVal = string.Format("{0:G5}", answer);
// output

Answered By – haldo

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *