Engineering notation in C#?

前端 未结 9 2199
离开以前
离开以前 2020-12-03 17:44

Is there any code out there (or a built-in function) which allows outputting a floating point number in engineering notation?

For example, 1.5e-4 would

9条回答
  •  悲&欢浪女
    2020-12-03 18:11

    Here is another version that handles negative and without rounding

    public static string ToEngineering(this double value)
    {
        var absValue = Math.Abs(value);
        var exp = absValue < 0.001 ? 0 : (int)(Math.Floor(Math.Log10(absValue) / 3.0) * 3.0);
        var newValue = value * Math.Pow(10.0, -exp);
        return $"{newValue}e{exp}";
    }
    

提交回复
热议问题