variable decimal places in .Net string formatters?

后端 未结 8 1540
感动是毒
感动是毒 2020-12-29 04:49

Fixed decimal places is easy

String.Format(\"{0:F1}\", 654.321);

gives

654.3

How do I feed the number of

8条回答
  •  [愿得一人]
    2020-12-29 05:35

    The string to format doesn't have to be a constant.

    int numberOfDecimalPlaces = 2;
    string formatString = String.Concat("{0:F", numberOfDecimalPlaces, "}");
    String.Format(formatString, 654.321);
    

提交回复
热议问题