VB.NETでのIntgerの変換は銀行丸めに注意

VisualBasic.NETでは、Double型等の小数点のあるものを変換すると、切り捨てられずに銀行丸めになる。

C#では、12345.9999といった小数点をint型に変換すると、小数点以下は切り捨てられて12345となる。

しかし、VB.NETでは12346となる。

文字列を直接CIntでコンバートしていたあたりから、なんだかたまに不明な挙動をするので、色々とテストしてみた。

なんで、こんな仕様にしたのかは知らないが、知らないでC#と同じように考えているとやられる。

たんに、小数点以下を切り捨てたい場合は、Double型をTrancateするのがベストだと思う。

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">