`decimal `

not `int`

and format the decimals when showing in your UI to show integers only (format string = "0").
See more:

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 %

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 %

Comments

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").
Comments

+5 for using decimal

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:

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.

Copy Code

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.

v3

Comments

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.

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.

Uh yeah I forgot to multiply by 100 first. (The OP wants 0-100, not 0-1.) Thanks.

dhdhdhdh

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

So if you have

In comparison if you always wanted 2 decimal places (even if they are 0's) you can code like this

So if you have

C#

Copy Code

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")`

v3

percentage should be calculate by below formula

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

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

Comments

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

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

Pls try

int resultAmtPer = (int)(0.5f + ((100f * definedPerAmt) / TotalAmt));

int avgPer = 100 - resultAmtPer;

int resultAmtPer = (int)(0.5f + ((100f * definedPerAmt) / TotalAmt));

int avgPer = 100 - resultAmtPer;

This will give ans as zero.

if (112/200)*100 will result zero

if (112/200)*100 will result zero

(count/totalcount)*100 is mathematically synonymous with (count * 100) / totalcount

your problem is resolved or not?

yes logically there is no difference but i found answer zero, no my problem is still there

try to declare int as int32 i am not sure about it but check?

while showing at ui just convert your count value to TOstring(); it will resolve your problem

LOGICALLY THERE IS A HUGE DIFFERENCE !

The division before multiplying by 100 will (for example) always yield zero if count is less than total count. Multiplying by 100 at that point is a waste of time.

You multiply first, then divide, so that the value will not always be zero.

The division before multiplying by 100 will (for example) always yield zero if count is less than total count. Multiplying by 100 at that point is a waste of time.

You multiply first, then divide, so that the value will not always be zero.

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

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

double fifty = (1/2) * 100;

This code sets

fiftyto 0 not 50! Why? Because of integer division.If you want something nailed use hammer, if you want something sliced use a knife...