I am using the following code to work out the percentages of two numbers, could some on please help me remove the decimal numbers off pcntNo and pcntYes
//Work out percentages
int yes = [currentYes intValue];
int no = [currentNo intValue];
int total = yes + no;
int pcntYes = (yes / total) * 100;
int pcntNo = (no / total) * 100;
It always returns 0
Also i want it with no decimal places if that is possible
Thanks
What's "decimal numbers"? If you mean fractional part, you won't have those - your percentages are stored as integers. By the way, you want slightly different arithmetics:
int pcntYes = (yes * 100) / total;
int pcntNo = (no *100) / total;
Otherwise, integer division will yield only zeros.
Use the floorf function from math.h:
int pcntYes = floorf((yes / total) * 100);
Related
So here is my code:
int var1 = serverList[i].webUp;
int var2 = serverList[i].webUp + serverList[i].webDown;
double ratio = (double)var1 / var2;
ratio = ratio * 100.0;
string percentage = string.Format("Up time is {0:P2}%", ratio);
Every time I run my application I keep getting NaN instead of a percent. webUp and webDown are both int, and I'm type casting to double when I do the division. Any ideas? I'm not dividing by 0, all values are greater than 0 and never in the negative. I keep getting "Up time is NaN%"
I have a double value as 47. I divide it by 60, but when I try to print it it shows 0.0000.
It should display 0.7858
How can I do that? Any help?
Here is my code:
int minutes = decimal * 60;
float min = (minutes)/60;
NSLog(#"%.4f",min);
Try this :
double decimal = 47.0 / 60.0; // Just so minutes is 47
double minutes = decimal * 60.0;
double min = minutes / 60.0;
NSLog(#"%.4f",min);
In Objective C (as well as C and C++), integer division is applied when both left and right operands are integers, so minutes / 60 = 0. If you want a floating point operation, use floating points literals.
try this:
int minutes = 40;
float min = (minutes * 1.0f)/60.0f;
NSLog(#"%.4f",min);
or
int minutes = 40;
float min = (float)minutes/60.0f;
NSLog(#"%.4f",min);
I have a float value i want to rounded off to nearest total i have find some math function but not working
Now potential profit is 300.52 so how to roundded of this please
float potentialprofit=otherthanCereniaAnnual*totalProfit;
NSString*potentialProfitTitle=[NSString stringWithFormat:#"%.2f",potentialprofit];
[potentialProfit setTitle:potentialProfitTitle forState:UIControlStateNormal];
int result = (int)ceilf(myFloat );
int result = (int)roundf(myFloat );
int result = (int)floor(myFloat);
float result = ceilf(myFloat );
float result = roundf(myFloat );
float result = floor(myFloat);
I think it will be helpful to you.
try this:
float f = ...;
f = (int)(f+0.5);
if you want to handle negative value, try this:
f = (int)(f < 0 ? f-0.5 : f+0.5);
I am having an issue with using a float in a UILabel.
float doubleNum;
floatNum = 10 / 20;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];
If I use "floatNum = 10 / 10;" it correctly returns "1.000000000", however, if I put in "floatNum = 10 / 20" it returns "0.0000000". I have tried about everything I know and it does not work. I know it's a dumb mistake, but I can't figure it out.
Happy Holidays. :)
You need to cast one of the integer's to a float.
Try replacing the divisional line with:
float floatNum = (float) 10 / 20;
and you should get the correct answer.
Or if possible just use floats in your division:
float floatNum = 10.0f / 20.0f;
should also work
The issue here is that you are assigning floatNum the result of dividing one INTEGER by another. The result of 10 / 20 is indeed 0 and as a float, it appears as 0.0000000. In order to obtain a proper result, you need to either use a cast type to turn it into a float or add a .0 to one of the numbers. In division, if one of the numbers is a float (which is easily done by just adding a .0 to one of them), the result will be a float as well.
Normally, C performs "integer division" (basically, division without the remainder -- 10/3 is 3R1, so it yields 3).
When you type floatNum = 10/20, it does 10/20 = 0 (remainder 10).
To fix this, you have to tell the program that you're giving it floating point numbers. Try using:
floatNum = 10.0 / 20,
floatNum = 10 / 20.0, or
floatNum = float (10 / 20).
All of those should work.
try floatNum = 10.0f/20.0f (i.e. make sure the calculation is being done with floats rather than ints)
Consider following example to understand how floats work:
float a = 1/120;
float b = 1.0/120;
float c = 1.0/120.0;
float d = 1.0f/120.0f;
NSLog(#"Value of A:%f B:%f C:%f D:%f",a,b,c,d);
Output: Value of A:0.000000 B:0.008333 C:0.008333 D:0.008333
For float variable a : int / int yields integer which you are assigning to float and printing it so 0.0000000
For float variable b: float / int yields float, assigning to float and printing it 0.008333
For float variable c: float / float yields float, so 0.008333
Last one is more precise float. Previous ones are of type double: all floating point values are stored as double data types unless the value is followed by an 'f' to specifically specify a float rather than as a double.
Change your code to:
float floatNum;
floatNum = 10.0f / 20.0f;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];
How might I round a float to the nearest integer in Objective-C:
Example:
float f = 45.698f;
int rounded = _______;
NSLog(#"the rounded float is %i",rounded);
should print "the rounded float is 46"
Use the C standard function family round(). roundf() for float, round() for double, and roundl() for long double. You can then cast the result to the integer type of your choice.
The recommended way is in this answer: https://stackoverflow.com/a/4702539/308315
Original answer:
cast it to an int after adding 0.5.
So
NSLog (#"the rounded float is %i", (int) (f + 0.5));
Edit: the way you asked for:
int rounded = (f + 0.5);
NSLog (#"the rounded float is %i", rounded);
For round float to nearest integer use roundf()
roundf(3.2) // 3
roundf(3.6) // 4
You can also use ceil() function for always get upper value from float.
ceil(3.2) // 4
ceil(3.6) // 4
And for lowest value floor()
floorf(3.2) //3
floorf(3.6) //3
The easiest way to round a float in objective-c is lroundf:
float yourFloat = 3.14;
int roundedFloat = lroundf(yourFloat);
NSLog(#"%d",roundedFloat);
Check the manual page for rint()
If in case you want round float value in integer below is the simple method for rounding the float value in objective C.
int roundedValue = roundf(Your float value);
let's do tried and checkout
//Your Number to Round (can be predefined or whatever you need it to be)
float numberToRound = 1.12345;
float min = ([ [[NSString alloc]initWithFormat:#"%.0f",numberToRound] floatValue]);
float max = min + 1;
float maxdif = max - numberToRound;
if (maxdif > .5) {
numberToRound = min;
}else{
numberToRound = max;
}
//numberToRound will now equal it's closest whole number (in this case, it's 1)