Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Strange rounding behavior

  Asked By: Conrad    Date: Oct 28    Category: Asp.net    Views: 1084
  

I'm having a very odd problem with .Net implicit rounding.
I'm trying to show an accurate percentage in a label control.


decimal rounded = Math.Round(Convert.ToDecimal(((totalCrashes * 100) / 60)), 1);

where totalCrashes == 1

I would like to return 1.6 from rounded, but I get 1.

I want to be able to show:

CrashLabel.Text = rounded.ToString("p")

and have it show: 1.6%

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Aaron Evans     Answered On: Oct 28

You need to divide by 60.0 with just 60 the result is an integer divide.
I would also change 100 to 100.0

 
Answer #2    Answered By: Salvatore Kelly     Answered On: Oct 28

Haven't done much .Net arithmetic, yet.
Never would have thought thought of
your solution. Works perfectly.

 
Didn't find what you were looking for? Find more on Strange rounding behavior Or get search suggestion and latest updates.




Tagged: