繁体   English   中英

将数字转换为科学计数法并获得指数

[英]Convert number to scientific notation and obtain exponent

如何将数字转换为科学计数法并获得指数? 例如,如果我有23582,并想将其转换为2.3582 x 10 ^ 4,然后获得数字的顺序“ 4”? (我正在使用C#)

请注意,如果数字小于1 ,则结果可能为负,因此我们使用Math.Floor处理该问题:

int exponent = num == 0 ? 0 : (int)Math.Floor((Math.Log10(Math.Abs(num))));

只需使用对数

  int exponent = value == 0
    ? 0 // special case: technically it should be -infinity 
    : (int) Math.Floor(Math.Log10(Math.Abs(value)));

您可以使用以下表示法,

int number = 23582;
            Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));

输出是

2.4E+04

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM