Stan
Stan

Reputation: 26511

How to always show decimals of float?

I've built console progress bar, it works just fine, but the problem is that on numbers with no decimals it does not print it. I need it to print 100,0 instead of 100. Probably easy task, but I don't know how.

float percent = (float)(progress * 100) / total;
Console.Write(" [{0}] [{1}]", userID, Math.Round(percent, 1));

Upvotes: 2

Views: 134

Answers (3)

Joe
Joe

Reputation: 82594

Fixed-Point

Console.Write(" [{0}] [{1:F1}]", userID, Math.Round(percent, 1));

Upvotes: 1

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171411

Console.Write(" [{0}] [{1}]", userID, Math.Round(percent, 1).ToString("N1")); 

Upvotes: 0

CassOnMars
CassOnMars

Reputation: 6181

Try using " [{0}] [{1:0.0}]" instead:

Console.Write(" [{0}] [{1:0.0}]", userID, Math.Round(percent, 1));

Upvotes: 3

Related Questions