Rounding?

Hi
if uptime and downtime are ints and uptime = 2 and downtime = 1
How would I displayed it as a percentage (as Im trying to do)

I want it to say 66.66% and 33.33%

Thanks

decimal percentageworth;
            if ((uptime + downtime) != 0)
                percentageworth = 100 / (uptime + downtime);
            else
                percentageworth = 1;

            label1.Text = "Uptime is " + this.uptime.ToString() + "(" + Math.Round(uptime * percentageworth, 2) + "%)";
            label2.Text = "Downtime is " + this.downtime.ToString() + "(" + Math.Round(downtime * percentageworth, 2) + "%)";
           
amoranAsked:
Who is Participating?
 
neilpriceCommented:
Of course that gives you a percentage as a fraction of 1.

To get it to display 66.66 or 33.33 you would need to multiply by 100.

(So to display to console you could do this...)

            Console.WriteLine((percentDowntime * 100).ToString().Substring(0, 5));
            Console.WriteLine((percentUptime*100).ToString().Substring(0,5));

Neil
0
 
neilpriceCommented:
Hi,

Try

double percentUptime = (double)(uptime)/(double)(uptime + downtime);
double percentDowntime = (double)(downtime)/(double)(uptime + downtime);

Hope this helps,
Neil
0
 
amoranAuthor Commented:
Neil,
That gave me
NaN
and
0.33333333333333333333%
?!
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
amoranAuthor Commented:
Ok

This works

label1.Text = "Uptime is " + this.uptime.ToString() + "/" + (uptime + downtime).ToString() + " (" + Math.Round(((double)(uptime) / (double)(uptime + downtime)) * 100, 2).ToString() + "%)";
            label2.Text = "Downtime is " + this.downtime.ToString() + "/" + (uptime + downtime).ToString() + "(" + Math.Round(((double)(downtime) / (double)(uptime + downtime)) * 100, 2).ToString() + "%)";
           

just not sure what NaN means.
0
 
neilpriceCommented:
Or you could try...

 string percentUptime;
            string percentDowntime;

            if(uptime!=0)
            {
                percentUptime = (((double)(uptime) / (double)(uptime + downtime)) * 100).ToString();
                if (percentUptime.Length > 5)
                    percentUptime = percentUptime.Substring(0, 5);
            }
            else
            {
                percentUptime="0";
            }
            if(downtime!=0)
            {
                percentDowntime = (((double)(downtime) / (double)(uptime + downtime)) * 100).ToString();
                if (percentDowntime.Length > 5)
                    percentDowntime = percentDowntime.Substring(0, 5);
            }
            else
            {
                percentDowntime = "0";
            }
            percentUptime += "%";
            percentDowntime += "%";

Which will check for divide by 0 errors and format the string to just two decimal places as you want.

Neil
0
 
neilpriceCommented:
NaN generally means Not a Number - you can get it dividing 0 by 0.

see: http://msdn2.microsoft.com/en-us/library/system.double.nan.aspx

Neil
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.