I must be doing something really obviously wrong, but I can't see it.
A double is a C type, not an Objective-C object. Hence you use C casts:
double myDouble = 3.2;
int myInt = (int)myDouble;
Just converting mentioned above is good enough though you might want to use floor() or ceil() functions before that.
intValue is a method for a NSNumber instance.
For scale type like int, double, and float, they are not class type. So, they have no methods. Some languages like C# may wrap int, or double as a object, and they can be transfered to each other by a sub-routine.
try this
NSInteger number=33;
NSUInteger count = (NSInteger)[number];
here, NSUInteger is long.
number is NSInteger
double myDouble = 3.2;
int myInt = #(myDouble).intValue;
Sample of code I actually use:
NSNumber * percentLike1 = #(#(self.percentLike.doubleValue*100).integerValue);
Related
I have a double that I need only the value of everything before the decimal point.
Currently I am using
NSString *level = [NSString stringWithFormat:#"%.1f",doubleLevel];
but when given a value of 9.96, this returns "10". So it is rounding. I need it to return only the "9". (note - when the value is 9.95, it correctly returns the "9" value.)
Any suggestions?
Thank You.
Simply assign the float/double value to a int value.
int intValue = doubleLevel;
Cast that baby as an int.
int castedDouble = doubleLevel;
Anything after the . in the double will be truncated.
9.1239809384 --> 9
123.90454980 --> 123
No rounding, simple truncation.
If you want to keep it as a float:
CGFloat f = 9.99;
f = floorf(f);
there are quite a variety of floor and round implementations.
they have been around since UN*X, and are actually part of those low-level libraries, be they BSD, Posix, or some other variety - you should make yourself familiar with them.
there are different versions for different "depths" of floating point variables.
NSString *level = [NSString stringWithFormat:#"%d",doubleLevel];
Very simple question here. I have a double that I wish to convert back to a NSInteger, truncating to the units place. How would I do that?
Truncation is an implicit conversion:
NSInteger theInteger = theDouble;
That's assuming you're not checking the value is within NSInteger's range. If you want to do that, you'll have to add some branching:
NSInteger theInteger = 0;
if (theDouble > NSIntegerMax) {
// ...
} else if (theDouble < NSIntegerMin) {
// ...
} else {
theInteger = theDouble;
}
NSInteger is a typedef for a C type. So you can just do:
double originalNumber;
NSInteger integerNumber = (NSInteger)originalNumber;
Which, per the C spec, will truncate originalNumber.
but anyway, assuming you want no rounding, i believe this should work simply
double myDouble = 10.4223;
NSInteger myInt = myDouble;
edit for rounding: (i'm sure theres a much simpler (and precise) way to do this.. (this also doesn't account for negative numbers or maximum boundaries)
double myDecimal = myDouble - myInt;
if(myDecimal < 0.50)
{
//do nothing
}
else
{
myInt = myInt + 1;
}
NSInteger is a typedef, it's the same as using an int. Just assign the value like:
double d;
NSInteger i = d;
JesseNaugher mentions rounding and I note the OP needs were met with a simple truncate, but in the spirit of full generalisation it's worth remembering the simple trick of adding 0.5 to the double before invoking floor() to achieve rounding. Extending Jonathan Grynspan's comment: NSInteger myInt = floor(myDouble + 0.5); i.e., rounding up in absolute terms. If rounding 'up' means rounding away from zero a more convoluted approach is needed: NSInteger myInt = floor( myDouble + (myDouble < 0.0 ? -0.5 : 0.5) );
I need to assign 2,554,416,000 to a variable. What would be the primitive to use, and what would be the object representation class to use? Thanks.
Chuck is right, but in answer to the "object representation", you want NSNumber used with the unsignedInt methods.
NSNumber *myNum = [NSNumber numberWithUnsignedInt:2554416000];
NSUInteger myInt = [myNum unsignedIntValue];
2,554,416,000 = 0x9841,4B80 ≤ 0xFFFF,FFFF (UINT_MAX), so uint32_t (unsigned int) or int64_t (long long).
A signed int32_t (int) cannot represent this because 0x9841,4B80 > 0x7FFF,FFFF (INT_MAX). Storing it in an int will make it negative.
This can be represented by a 32-bit unsigned integer (UINT_MAX is about 4 billion). That's actually what NSUInteger is on the iPhone, but if you want to be very specific about the bit width, you could specify a uint32_t.
You could store it in a regular int scaled down by 1000 if you wanted, if this represented a score that could never have the bottom 3 digits hold any info or something similiar. This would be a way to save a few bits and possibly an entire extra int of space, if that matters.
I have strings that look about like this:
stringA = #"29.88";
stringB = #"2564";
stringC = #"12";
stringD = #"-2";
what is the best way to convert them so they can all be used in the same mathmatical formula?? that includes add, subtract.multiply,divide etc
Probably floatValue (as it appears you want floating-point values), though integerValue may also be of use (both are instance methods of NSString).
[stringA doubleValue]
These are all wrong, because they don't handle errors well. You really want an NSNumberFormatter.
If you have the string #"abc" and try to use intValue or floatValue on it, you'll get 0.0, which is obviously incorrect. If you parse it with an NSNumberFormatter, you'll get nil, which is very easy to distinguish from an NSNumber (which is what would be returned if it was able to parse a number).
Assuming that you have NSString variables.
NSString *stringA = #"29.88";
NSString *stringB = #"2564";
NSString *stringC = #"12";
NSString *stringD = #"-2";
suppose, you want to convert a string value to float value, use following statement.
float x=[stringA floatValue];
suppose, you want to convert a string value to integer value, use following statement.
NSInteger y = [stringC intValue];
Hope, it helps to you.
Being a starting Objective-C developer, and not having the patience to actually do some studying, rather than just diving into things, I ran into the following:
I have a CGFloat, and I want to divide it by something, and use the result as an NSInteger.
Example:
CGPoint p = scrollView.contentOffset; //where p is a CGFloat by nature
NSInteger * myIndex = p.x/468.0;
While compiling, I get the error: "Incompatible types in initialization".
I guess that's because of the mismatch between the CGFloat and the NSInteger.
What should I know to get out of this? Sorry for bothering you.
Two problems. You're using a NSInteger pointer (NSInteger *myIndex). And you'll need a cast to go from float to int. Like so:
NSInteger myIndex = (int)(p.x / 468.0);
NSInteger is not an actual object type. It is a typedef for a primitive integer type. Remove the * and your example should work. As it is now, you're trying to assign the result of your math as a pointer and you're getting that error message.