Double scientific notation format 10th power C#

…衆ロ難τιáo~ 提交于 2019-12-12 03:42: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.



来源:https://stackoverflow.com/questions/39785578/double-scientific-notation-format-10th-power-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!