Why does .Net use a rounding algorithm in String.Format that is inconsistent with the default Math.Round() algorithm?

前端 未结 4 945
夕颜
夕颜 2020-12-29 21:20

I\'ve noticed the following inconsistency in C#/.NET. I was wondering why it is so.

Console.WriteLine(\"{0,-4:#.0} | {1,-4:#.0}\", 1.04, Math.Round(1.04, 1)         


        
4条回答
  •  情书的邮戳
    2020-12-29 21:26

    As a historical note, the original Visual Basic implementation of Format$ also was inconsistent with round-to-even, aka, Banker's Rounding. The original Format$ code was written by Tim Paterson. You might recall that Tim was the author of a little program called QDOS (later known as MS-DOS) that was rather a good seller for a while there.

    Perhaps this is yet another case of 25 years of backwards compatibility.

提交回复
热议问题