Home > OS >  C# format float to show 2 places before and after decimal point
C# format float to show 2 places before and after decimal point

Time:01-02

I have a time board and want to show time as 01:24 for example. My current code:

timeInGame.ToString("F2").Replace(".",":");

This code returns 1:24 .

CodePudding user response:

Specify the format explicitly:

timeInGame.ToString("00.00").Replace(".",":");

Note however that it is incorrect to pretend that decimal numbers map to minutes:seconds or hours:minutes, e.g. 1.5 == 1:30.

  • Related