Click here to Skip to main content
11,641,971 members (64,002 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi
i am using int32 variable name 'count' and 'totalcount' to get the total number of students for boys and girls. the total number is found fine,
i want to calculate the %age also

count = ((count * 100) / totalcount);

it generates the resultant number but i want the round figure ie 29.8 is displayed as 29, it is making problem in completing the total 100 %
Posted 6-Sep-11 20:55pm
Comments
koolprasad2003 at 7-Sep-11 3:11am
   
Do u want Round of value ? use "System.Math.Round(//val));"
yesotaso at 7-Sep-11 17:37pm
   
Common C Programming Errors or you can say "C alike"...
double fifty = (1/2) * 100;
This code sets fifty to 0 not 50! Why? Because of integer division.
If you want something nailed use hammer, if you want something sliced use a knife...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To have the totals add up to 100, your in best bet is to keep the calculations decimal not int and format the decimals when showing in your UI to show integers only (format string = "0").
  Permalink  
Comments
lukeer at 7-Sep-11 6:17am
   
+5 for using decimal
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

A general solution to ensure that a rounded collection of numbers still adds up to the same as it did before is quite difficult. However, I think you are just doing two numbers, so one is 100% minus the other:
int boys_pc = (int)(0.5f + ((100f * boys_count) / total_count));
int girls_pc = 100 - boys_pc;

Cast to float to avoid integer division giving you a zero, and add 0.5 for integer rounding. Use 0.5f not 0.5 to keep it from expanding to double (which is overkill for something which will be rounded to int anyway).

Edit: If you have to multiply anyway, just multiply by a float constant (e.g. 100f not 100) to avoid integer division.
  Permalink  
v3
Comments
lukeer at 7-Sep-11 6:15am
   
I think there is an error in your calculation.
Think of 80 boys out of 100 students.
That would be a rate of 0.8 (innermost parentheses). Adding 0.5F would exceed 100%. Hence, girls percentage would go negative.
BobJanova at 7-Sep-11 13:12pm
   
Uh yeah I forgot to multiply by 100 first. (The OP wants 0-100, not 0-1.) Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

percentage   should be calculate by below formula

count = ((count / totalcount)*100);
  Permalink  
Comments
Db issues at 7-Sep-11 3:08am
   
the result is 0 in that case i tried before,
now i have declared double percent = ((count * 100) / totalcount);
it displays same result but how to round the figure and get the floor/ ceiling function
Anuja Pawar Indore at 7-Sep-11 3:18am
   
This will give ans as zero.
if (112/200)*100 will result zero
GParkings at 7-Sep-11 4:11am
   
(count/totalcount)*100 is mathematically synonymous with (count * 100) / totalcount
pooja 08 at 7-Sep-11 5:01am
   
your problem is resolved or not?
Db issues at 7-Sep-11 5:08am
   
yes logically there is no difference but i found answer zero, no my problem is still there
pooja 08 at 7-Sep-11 6:16am
   
try to declare int as int32 i am not sure about it but check?
pooja 08 at 7-Sep-11 6:32am
   
while showing at ui just convert your count value to TOstring(); it will resolve your problem
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use
System.Math.Round()
method.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use Math.Round(56.8) will be 57
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

You can use ToString to format how you want the display.
So if you have

double percent;
...
percent = 29.8;
 
...
 
public string PercentDisplay
{
    get{return percent.ToString("00")} //Will always show 2 digits and no decimal
}


In comparison if you always wanted 2 decimal places (even if they are 0's) you can code like this percent.ToString("00.00")
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

class Program
   {
       static void Main(string[] args)
       {
          
           for (int i = 1; i < 26; i++)
           {
               var percentage= ((double)i / 25)*100;
               Console.WriteLine(percentage);
 
           }
           Console.ReadLine();
       }
   }

This Will Provide the Percentage : Here we suppose total record is 25
  Permalink  
v2
Comments
Wes Aday at 7-Jul-15 7:20am
   
Do think that after 4 years the OP still waiting to find out how to do simple math?
varun2007joshi at 7-Jul-15 9:02am
   
No . But I have to answer Someone will get the help from this answer in future
Wes Aday at 7-Jul-15 9:24am
   
Well that is fine an dandy. However; the question was already answered 4 years ago and there is no reason at all to answer it again. And your solution does not answer the question asked.
varun2007joshi at 7-Jul-15 9:47am
   
Can u explain Why My code is Not Answered the Asked Question
Wes Aday at 7-Jul-15 15:18pm
   
If you had read the question you would have seen that he is not asking how to calculate a percentage. He is asking how to format the result.

And again, it was asked and answered 4 years ago. There is no reason at all for someone to come along now and provide yet another answer.
varun2007joshi at 8-Jul-15 1:50am
   
Ok . Mine Mistake

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 285
1 Sergey Alexandrovich Kryukov 159
2 Afzaal Ahmad Zeeshan 145
3 OriginalGriff 110
4 Mika Wendelius 90
0 Mika Wendelius 460
1 DamithSL 353
2 OriginalGriff 315
3 Sergey Alexandrovich Kryukov 179
4 Afzaal Ahmad Zeeshan 169


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 7 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100