Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Rounding?

Posted on 2006-06-01
7
Medium Priority
?
241 Views
Last Modified: 2010-04-16
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) + "%)";
           
0
Comment
Question by:amoran
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:neilprice
ID: 16807581
Hi,

Try

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

Hope this helps,
Neil
0
 
LVL 6

Accepted Solution

by:
neilprice earned 80 total points
ID: 16807611
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
 

Author Comment

by:amoran
ID: 16807615
Neil,
That gave me
NaN
and
0.33333333333333333333%
?!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:amoran
ID: 16807700
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
 
LVL 6

Expert Comment

by:neilprice
ID: 16807719
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
 
LVL 6

Expert Comment

by:neilprice
ID: 16807746
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question