Double scientific notation format 10th power C#

2019-09-08 03:37发布

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

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-08 04:01

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! :)

查看更多
Viruses.
3楼-- · 2019-09-08 04:03

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.

查看更多
登录 后发表回答