Why does formatting a DateTime as a string truncate and not round the milliseconds?

前端 未结 3 672
死守一世寂寞
死守一世寂寞 2021-01-08 01:09

When a Double is formatted as a string rounding is used. E.g.

Console.WriteLine(12345.6.ToString(\"F0\"));

outputs

<         


        
3条回答
  •  旧巷少年郎
    2021-01-08 01:51

    Old question, but it has been referred to from a new, and the answers discuss reasons for rounding or not (which of course are valid) but leaves the question with no answer.

    The reason for not rounding is, that ToString just prints the date/time parts you ask for.

    Thus, for example, neither will it round to the nearest minute:

    Console.WriteLine(dateTime.ToString("yyyy-MM-hhThh:mm", ci));
    

    Output:

    2011-09-03T03:18
    

    With no parameter, ToString uses the default date/time format string of your environment.

提交回复
热议问题