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

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

Related

How to divide two integers without getting 0? [duplicate]

This question already has answers here:
Division of integers returns 0
(2 answers)
Closed 10 months ago.
My goal is two divide two integers in Presto 0.212, e. g. 1/2. The naive approach SELECT 1/2 returns 0. Next, I tried SELECT CAST(1/2 AS DOUBLE), but this also returns 0. How to divide 1/2 such that 0.5 is returned?
I'm not familiar with Presto, but my guess is that in the example you've provided 1/2 is being evaluated as an integer then is being cast as a double. Maybe something along the lines of SELECT CAST(1 AS DOUBLE)/CAST(2 AS DOUBLE) or you maybe you could just add .0 to the end of your numbers like SELECT 1.0/2.0. Just a few shots in the dark from me.

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

Rounding issue when using NSString and %f [duplicate]

I have a floating point number that have more decimal digits, for example:
float fRes = 10.0 / 3.0;
actually the fRes value is 3.3333333333333
it's possible set for example 2 decimal digits:
float fRes = 10.0 / 3.0;
// fRes is 3.333333333333333333333333
float fResOk = FuncRound( fRes, 2 );
// fResOk is 3.33
thanks in advance
I don't know where you are using this rounded number, but you should only round your value when displaying it to the user, there are C based format string ways to round floating point numbers for example
[NSString stringWithFormat:#"%.2f", value];
as you may have already read, floating point number are approximations of real numbers, so doing fResOk = roundf( fRes*100.0)/100.0; may not give you 3.33 but a number which is just as close as you can get with floating point number to 3.33.
Assuming that you're looking for the correct function to round to a certain number of digits, you'll probably find it easiest to do the following:
fResOk = roundf( fRes*100.0)/100.0;
That will multiply the value by 100 (giving you your 2 digits of significance), round the value, and then reduce it back to the magnitude you originally started with.

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;