Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

C# Can you store the string format in a variable?

I have,

string text = $"RiseTime: Avg: {AvgRise:F5}, Max: {MaxRise:F5}, Min: {MinRise:F5} 
 ";

I would like to change the F5 formatting during runtime to maybe F6 of E4 or something else. How can I do this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Charlieface points out, you need to readjust how you do things. Instead of using the inline formatting, use string.format

int decimalPlaces= 5;
string fmt="RiseTime: Avg: {0:F" + decimalPlaces.ToString() + "}, Max: {1:F" + decimalPlaces.ToString() +"}, Min: {2:F"+ decimalPlaces.ToString() +"} 
 ";
string result=string.format(fmt, AveRise, MaxRise, MinRise);

Obviously using this method you can change the variable "fmt" as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...