Double scientific notation format 10th power C#

2019-09-08 03:48发布

问题:

I am having double value = 30308950000;

I want to display it like 3.03x10^10

How can I achieve it using C#?

I have tried

double value = 30308950000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));

output = 3.03E+10

I dont want the 3.03E+10 format but 3.03x10^10. Thanks

回答1:

Easy fix:

double value = 30308950000;
Console.WriteLine(
    value.ToString("0.###E+0", CultureInfo.InvariantCulture)
        .Replace("E-","x10^-")
        .Replace("E+","x10^"));

Shout out to Jon Skeet! :)



回答2:

Sadly, NumberFormarInfo doesn't provide any ExponentialSign parameter.

That it, the Exponential Format Specifier (E) use only NegativeSign, NumberDecimalSeparator and PositiveSign information from the NumberFormarInfo.

To do it "in the pattern", you have to use your own implementation of ICustomFormatter like here.