Somewhat easier way to show percentage

Hello,

First off, I love the course.

But maybe it’s easier to title the progressbar like this:

if (count > 0)
{
    taskProgressBar.value = completed / (float) count;
}
else
{
    taskProgressBar.value = 1;
}

taskProgressBar.title = $"{taskProgressBar.value * 100:F1} %";
2 Likes

Good solution!

There are often many paths to the same goal.

There’s even a format string for percentages with the same option to specify the number of decimal places. It puts a space between the number and the %, though.

this.tasksProgressBar.title = $"Completed: {progress:P1}";

image

4 Likes

One more way to write this:

taskProgressBar.title = $"{progress * 100:0.##} %";

This way, 3/6 will visualize as “50%” (decimal point, because not necessary) while 4/6 will visualize as "66.67%

2 Likes

Privacy & Terms