DotNetKick.com is an open-source project. Please report any bugs and let us know your great suggestions. Currently running svn revision 620 (rss)

Kick Spy!, Kick Zeitgeist and Kick Widgets

24
kicks
published 3 months, 23 days ago, submitted by shaharab 3 months, 23 days ago

devmuse.com — Casting float to int is interesting. Not only it causes the value to get truncated, but it also changes binary representation. When you want to cast float to int you have two options:

tags: , | tag it

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:

Comments:
I wouldn't say it's dangerous per se, but it's definitely interesting. I would have never expected Convert.ToInt32 would actually perform a rounding operation.
posted by RPenton RPenton 3 months, 23 days ago
I believe this is because it uses the round to even method (http://en.wikipedia.org/wiki/Rounding#Round-to-even_method)
posted by johnrummell johnrummell 3 months, 22 days ago
You are right jrummell, as written in the link that you provided this is called “bankers’ rounding”.
I understand why banks need this behaviour (to be fair), what i can’t understand is why should we have this unexpected behaviour in the .Net framework.
posted by shaharyr 3 months, 22 days ago
More detail on the surprising way default rounding works in .NET - http://www.dotnetkicks.com/csharp/Banker_s_Rounding_vs_Common_Rounding_in_NET
posted by powerrush powerrush 3 months, 22 days ago
Another potential gotcha when using nullables: Convert.ToInt32(x) returns zero when x==null. Same for ToInt64(), ToSingle(), etc.
posted by chinhdo 3 months, 18 days ago



information Login or create an account to comment on this story
 

Sponsored Link: www.carlist.ie

Search:

Ads via The Lounge