Home > OS >  How to display 100 from 99.99 from stringformat
How to display 100 from 99.99 from stringformat

Time:10-23

I would like to have it round up to 100 using stringformat, not math.round because the way in the code I can't use math.round.

CodePudding user response:

If you set your precision to a single digit like this, the number will be rounded up to 100.0. You can use string interpolation and not have to specify String.Format like this:

Console.WriteLine($"{99.99:0.0}");

CodePudding user response:

If you want to round the number to the nearest integer, you can try:

var str = string.Format("{0:.}", 99.99); // Will return "100"

If you want to always have one digit after the delimiter, you can change it to:

var str = string.Format("{0:.0}", 99.99); // Will return "100.0"

You can always check the official documentation for the entire set of options: String.Format Method

  •  Tags:  
  • c#
  • Related