Calculation returns 0 objective-c [duplicate] - objective-c

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 9 years ago.
I'm trying to calculate an average where i am getting the value from a textfield and multiplying it by the value of a label.
int grade1 = [[self.Cw1Grade text]intValue];
int grade1weight = self.weight1.text.intValue;
int a1grade = grade1 / 100;
int a1total = a1grade * grade1weight;
NSString *grade1total = [NSString stringWithFormat:#"%d", a1total];
[self.averageLabel setText:grade1total];
help appreciated thanks for your time

You can't divide integers like that. Or well, you can, but you won't get the result you expect, because integer divisions return integers (result will be rounded down to the next whole number)
Try this:
float a1grade = (float)grade1 / 100;
or
float a1grade = grade1 / 100.0;
If at least one of the operands is a float, you'll have a floating point division. But of course you have to store the result in a float variable in that case.
Also don't forget, that the string format specifier for floats is %f, not %d.

Related

Why do these float calculations return different values? [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 9 years ago.
I'm working with an iOS project where I have to do a bit of math. Can anyone explain to me why these two implementations return different results?
float total = 31/30;
NSLog(#"%f", total); // returns 1.00000 in console
float total2 = 31/30.0;
NSLog(#"%f", total2); // returns 1.03333 in console
In the majority of computer languages, division involving two integers will have an integer result, the floor of the real result.
In C division, the type of the result is the type of the most precise number in the calculation. In your first example, both 31 and 30 are integers, and so the result is then the integer 1 which is cast to a float to result in 1.00. In your second example, while 31 is an integer, 30.0 is a literal float, and the calculation has a float result, which is than stored in your variable (1.033333...).

Calculate positive fractions in objective-c [duplicate]

This question already has answers here:
Objective c division of two ints
(4 answers)
Closed 9 years ago.
I tried to calculate 4/3 and store it into a float.
float answer = 4/3;
This only returns 1. Isn't objective-c able to calculate these kinds of fractions or do I have to do it any other way?
If numerator and denominator are both integers, then division will be integer. Use
float answer = 4/(float)3
4 and 3 are integers. So that division is an integer division, which evaluates to 1.
If you want a floating-point division, use (at least one) float literal.
float answer = 4f/3;
Your assignment contains an integer divide, which returns zero if the number you are dividing by is greater. You probably meant to do:
float p1 = (4.0f / 3.0f);
or
float p1 = ((float)4 / 3);

Objective-C. Math functions

I have a variable which contain the textfield value. I want to multiply that x value with a decimal number like 0.013. But after multiplication I got as answer 0.
It takes the decimal value as 0. What is the reason?
Once you get the text from the textfield, convert the string using floatValue :
CGFloat val = [myValue floatValue];
CGFloat res = val * 0.013;
In a comment, the OP notes
If i use 4/3 then it only takes answer as 1
This suggests that the problem is in how the value is initialized: 4/3 is integer division, returning the int value 1. The solution is to be sure that the calculations are actually dealing with floats, start to finish, by using float literals, e.g., replacing 4/3 with 4.0/3.0
you need to convert the text to the required format:
float floatValue = [yourTextField floatValue];
int intValue = [yourTextField intValuew];

In objective c from a float number I want to sepatate the decimals to do further math work on

I am setting up a calculator that will generate a float number after the user's input. As an example: if the answer generated is 1.75 that would actually represents 1.75 hours. I want to split the answer into two parts to read 1 hr. 45 min which will be more user friendly than .75 of an hour is.
So I need to be able to delete the whole digit(s) (in this example the "1") before the decimal to get a variable value using the .75 --kind of like this:
mIn = (.75 * 60) which would give me the 45 minutes value to put in my...
[NSString stringWithFormat:#"#% hr. #% min", hRs, mIn]; type of statement
Any help on this would be greatly appreciated.
You almost certainly do not want to use a float to store a user-entered number, particularly for a calculator. This will lead to decimal/binary rounding errors that will drive you crazy. You want to use NSDecimalNumber for this kind of work.
The other answers here will work, but you want to replace floor() with decimalNumberByRoundingAccordingToBehavior:. You'll use an NSDecimalNumberHandler with a rounding mode of NSRoundDown and an appropriate scale (probably 2 or 3 for your purposes).
Use floor(floatValue) to get the integer portion, subtract that from the floatValue to get the decimal portion.
Ex:
float floatValue = 1.75;
float integerPortion = floor(floatValue);
float decimalPortion = floatValue - integerPortion;
NSString *timeString = [NSString stringWithFormat:#"%.0f hr. %.0f min", integerPortion, decimalPortion * 60];
NSLog(#"timeString: %#", timeString);
NSLog output:
timeString: 1 hr. 45 min
You can cast the floating value to an int and subtract that from the value or you can floor it.
float time = 1.75;
int minutes = (time - (int)time) * 60; //or (time - floor(time)) * 60;
NSLog(#"Time: %#", [NSString stringWithFormat:#"%d hr. %d min", (int)time, minutes]);

How to round of the value? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Objective-c: How to round off Float values?
I wanted to roundoff the float value from 42.56789 to 42.6. Can any one help me out in this.
Thanks in advance
There are two functions in c namely floor and ceil to roundoff the float values.
Refer Wikipedia reference
float value=42.56789;
NSString *roundedValue=[NSString stringWithFormat:#"%0.f",value];
NSLog(#"%#",roundedValue);
It prints 43 on console.
float f = 42.56789;
f *= 10.0f;
f = ceilf(f);
f /= 10.0f;
NSLog(#"%.02f",f);
I have done it manually .
float val=42.567890// float gives 6 decimal places
int x=(int) val;
float f=val-(int)val;
int o=(int) f*10;
float round=x+o/10+(f-(o/10))*1000)/50;