How to round of the value? [duplicate] - objective-c

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;

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);

Calculation returns 0 objective-c [duplicate]

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.

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];

String formatting: less decimals in %f float? [duplicate]

This question already has answers here:
Make a float only show two decimal places
(13 answers)
Closed 8 years ago.
Well, I am using %f for displaying float values in my string. However, the values are too accurate (around 6 decimal digits after the point). I'd like to only display 1 decimal. But how?
%0.2f or some other number after the decimal point.
NSLog(#"%0.1f", floatThing);
I think that should work. For more decimals change 0.1 to 0.2, 0.3, 0.4 ... etc.
[NSString stringWithFormat:#"%.DECIMAL_PRECISION_NUMBER_HEREf", someFloat]
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
and
http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html