variable decimal places in .Net string formatters?

后端 未结 8 1560
感动是毒
感动是毒 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条回答
  •  猫巷女王i
    2020-12-29 05:46

    I used two interpolated strings (a variant of Michael's answer):

    double temperatureValue = 23.456;
    int numberOfDecimalPlaces = 2;
    
    string temperature = $"{temperatureValue.ToString($"F{numberOfDecimalPlaces}")} \u00B0C";
    

提交回复
热议问题