2

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.

Jane wang
  • 298
  • 1
  • 3
  • 16

2 Answers2

4

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}");
1

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

Volodymyr
  • 257
  • 2
  • 5