Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading