Why is using a NON-decimal data type bad for money?

前端 未结 5 1627
执念已碎
执念已碎 2021-02-08 23:13

tl;dr: What\'s wrong with my Cur (currency) structure?

tl;dr 2: Read the rest of the question please, before giving an

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 23:57

    Cur c = 0.00015;
    System.Console.WriteLine(c);
    // rounds to 0.0001 instead of the expected 0.0002
    

    The problem is that 0.00015 in binary is really 0.00014999999999999998685946966947568625982967205345630645751953125, which rounds down, but the exact decimal value rounds up.

提交回复
热议问题