Formatting numbers with significant figures in C#

后端 未结 8 1453
小鲜肉
小鲜肉 2020-11-28 13:01

I have some decimal data that I am pushing into a SharePoint list where it is to be viewed. I\'d like to restrict the number of significant figures displayed in the result

8条回答
  •  时光说笑
    2020-11-28 13:53

    This might do the trick:

    
    double Input1 = 1234567;
    string Result1 = Convert.ToDouble(String.Format("{0:G3}",Input1)).ToString("R0");
    
    double Input2 = 0.012345;
    string Result2 = Convert.ToDouble(String.Format("{0:G3}", Input2)).ToString("R6");
    

    Changing the G3 to G4 produces the oddest result though. It appears to round up the significant digits?

提交回复
热议问题