NSNumber decimal to C primitive value? - objective-c

I want to convert a decimal NSNumber to an int or other form which I can do math with. Here's the annotated code for my project:
NSNumber *Left = [left valueForOutputKey:#"Y"];
This line gets a Quartz Composer Outlet, usually with a value around 0.512.
Basically, I want to multiply this by 10, and then do some operations like greater than and less than to see which range it is in.

Since it looks like you're dealing with a fractional component, you want to convert it to a float or a double to perform your operations, depending on how big you expect that value to be. A float should be fine unless you're dealing with ridiculously large or precise numbers. Here's how it would look, for example:
float lValue = [[left valueForOutputKey:#"Y"] floatValue];
lValue *= 10;
if (lValue < 10) {
// do whatever
}
else if (lValue > 50) {
// do whatever
}
Then to store the value back in your outlet or whatever, you pack it back into a NSNumber:
NSNumber *newValue = [NSNumber numberWithFloat:lValue];
[left setValue:newValue forKey:#"Y"];
You may have to convert newValue into a string to display it in a control, just use [newValue stringValue] to do that.

Use one of the methods of NSNumber:
int leftInt = [Left intValue];
float leftFloat = [Left floatValue];
double leftDouble = [Left doubleValue];

Related

Limiting Number of Decimals of an Output in Xcode 4.5

I have a few actions in xcode where the number of decimals in the output value needs to be limited to 3 decimal places out. What do I need to add to my code to achieve this task?
Here is an example of one of my actions:
- (IBAction)calculateMolarity:(id)sender {
float ourValue = [[_calcTextFieldNumOne text] floatValue] /[ [_calcTextFieldTwo text] floatValue];
NSNumber *ourNum =[NSNumber numberWithFloat:ourValue];
[_outputOfMolarity setText:[ourNum stringValue]];
NSString* formattedNumber = [NSString stringWithFormat:#"%.03f", ourNum];
here %.03f tells the formatter that you will be formatting a float
(%f) and, that should be rounded to three places, and should be padded
with 0's.
but you can do directly with your float ourValue like this
NSString* formattedNumber = [NSString stringWithFormat:#"%.03f", ourValue];
there is no need to convert your float value to NSNumber
you can use "%.3f" or "%.03f", no matter both gives same fromat
#"%.3f" = 1234.567
#"%.03f" = 1234.567 // which is equal to #"%.3f"

Floating Point things work.....Most of the time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Correcting floating point numbers
float randNum = arc4random()%(100)-1;
I read somewhere that this will give me random numbers between 1-100. Or something close to that.
This line seems to work all the time, but I convert this number to an NSString for storage/printing to text, and then convert it back to a float for ordering and other calculations. But when I get that float back sometimes it looks something like gibberish; like this in the variable view:
9 float 9.75303731e-41
Why would converting to an NSString and back to a float ruin the float value I'm using? (e.g. What could I have screwed up? Or should I be using CGFloat instead?)
I think this is all the pertinent code:
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
floatArray[30];
// put three random floats into an NSMutableArray of NSStrings
for(int i = 0; i < 3; i++)
{
float randNum = arc4random()%(100)-1;
NSString *randString = [NSString stringWithFormat:#"%.3f", randNum];
[stringArray addObject:randString];
}
// convert NSStrings back to float
for(NSString *string in stringArray)
{
float temp = [string floatValue];
floatArray[iterator] = temp;
}
Thanks in advance for any help/advice.
EDIT: When I step through the code it looks like the float value looks sane until the line "float temp = [string floatValue]", which is where the value seems to be garbaged.
Why are you using float when the result from arc4random() is a uint32_t? Switching to integer types would almost certainly get around all this, as I suspect the problem is because of the conversion to string form allowing only 3 significant digits. What happens if you use %.15f as your format?
Sidenote: use arc4random_uniform() - it's simpler and guaranteed to be a uniformly random distribution within that range.

double to int (or long, long long) conversion sometimes is not good

I am trying to convert quite big double number to int (or long or long long), but have some difficulties. Almost always it converts good, but not sometimes:
My code:
double price = 12345678.900000;
double hundredNumber = price * 100;
NSNumber *number = [NSNumber numberWithDouble:hundredNumber];
int tempNumber = [number intValue];
All goes good, until tempNumber. it logs out 1234567889, but it should be 1234567890 (...89 - ...90)
Does anyone know why it could happen and how to convert correctly?
P. S. I am trying to implement backspace to value (e.x. 123.45, after that it should be 12.34). Maybe anyone had implemented something like this?
You're always going to get the risk of rounding errors if you're using floating point numbers.
Why not always store prices as a long long?
i.e. instead of £5.50, store 550p. That way you will never have any rounding issues at all.
As commented, I would be careful with the roundings, because of the possible errors.
One possible solution is to work with doubles like Google does with coordinates in Android: multiplying them by 1E6. If you operate with integers then you'll safe much more CPU cycle than operating with doubles. Try this out:
double priceDouble = 33.f / 34.f;
NSLog(#"double: %f", priceDouble);
NSInteger priceInteger = (NSInteger)(priceDouble * 1E6);
NSLog(#"int: %d", priceInteger);
NSNumber * priceNumberWithDouble = [NSNumber numberWithDouble:priceInteger];
priceDouble = [priceNumberWithDouble doubleValue];
NSLog(#"double: %f", priceDouble);
NSNumber * priceNumberWithInteger = [NSNumber numberWithInteger:priceInteger];
priceInteger = [priceNumberWithInteger integerValue];
NSLog(#"int: %d", priceInteger);
double test = ((double)priceInteger)/1E6;
NSLog(#"Test: %f",test);
My output is the following:
double: 0.970588
int: 970588
double: 970588.000000
int: 970588
Test: 0.970588

Converting NSString to a decimal

I need to change the code below to make "intAmount" a decimal or an integer (i.e. a person can enter .10 or 1) in my uitextfield. The last line "myProduct" has to be a decimal not an integer and return the product in the format "18.00" for example. Can someone help someone help me alter my code snippit for this?
//amt has to be converted into a decimal value its a NSString now
NSInteger intAmount = [amt intValue];
//where total gets updated in the code with some whole (integer) value
NSInteger total=0;
//Change myProduct to a decimal with presicion of 2 (i.e. 12.65)
NSInteger myProduct=total*intAmount;
THIS DOESN'T WORK
NSDecimalNumber intAmount = [amt doubleValue];
//Keep in mind totalCost is an NSInteger
NSDecimalNumber total=totalCost*intAmount;
Use doubleValue instead of intValue to get the correct fractional number out of your text field. Put it in a variable of type double rather than NSInteger. Then use the format %.2g when you print it out and it will look like you want it to.
If you need to track decimal values explicitly, you can use NSDecimalNumber. However, if all you're doing is this one operation, Carl's solution is most likely adequate.
If you have a string representation of a real number (non-integer), you can use an NSScanner object to scan it into a double or float, or even an NSDecimal structure if that is your true intention (the NSDecimal struct and NSDecimalNumber class are useful for containing numbers that can be exactly represented in decimal).
NSString *amt = #"1.04";
NSScanner *aScanner = [NSScanner localizedScannerWithString:amt];
double theValue;
if ([aScanner scanDouble:&theValue])
{
// theValue should equal 1.04 (or thereabouts)
}
else
{
// the string could not be successfully interpreted
}
The benefit to using a localised NSScanner object is that the number is interpreted based on the user's locale, because “1.000” could mean either one-thousand or just one, depending on your locale.

How to convert a string into double and vice versa?

I want to convert a string into a double and after doing some math on it, convert it back to a string.
How do I do this in Objective-C?
Is there a way to round a double to the nearest integer too?
You can convert an NSString into a double with
double myDouble = [myString doubleValue];
Rounding to the nearest int can then be done as
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))
I'm honestly not sure if there's a more streamlined way to convert back into a string than
NSString* myNewString = [NSString stringWithFormat:#"%d", myInt];
To really convert from a string to a number properly, you need to use an instance of NSNumberFormatter configured for the locale from which you're reading the string.
Different locales will format numbers differently. For example, in some parts of the world, COMMA is used as a decimal separator while in others it is PERIOD — and the thousands separator (when used) is reversed. Except when it's a space. Or not present at all.
It really depends on the provenance of the input. The safest thing to do is configure an NSNumberFormatter for the way your input is formatted and use -[NSFormatter numberFromString:] to get an NSNumber from it. If you want to handle conversion errors, you can use -[NSFormatter getObjectValue:forString:range:error:] instead.
Adding to olliej's answer, you can convert from an int back to a string with NSNumber's stringValue:
[[NSNumber numberWithInt:myInt] stringValue]
stringValue on an NSNumber invokes descriptionWithLocale:nil, giving you a localized string representation of value. I'm not sure if [NSString stringWithFormat:#"%d",myInt] will give you a properly localized reprsentation of myInt.
Here's a working sample of NSNumberFormatter reading localized number String (xCode 3.2.4, osX 10.6), to save others the hours I've just spent messing around. Beware: while it can handle trailing blanks such as "8,765.4 ", this cannot handle leading white space and this cannot handle stray text characters. (Bad input strings: " 8" and "8q" and "8 q".)
NSString *tempStr = #"8,765.4";
// localization allows other thousands separators, also.
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
// next line is very important!
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // crucial
NSNumber *tempNum = [myNumFormatter numberFromString:tempStr];
NSLog(#"string '%#' gives NSNumber '%#' with intValue '%i'",
tempStr, tempNum, [tempNum intValue]);
[myNumFormatter release]; // good citizen
olliej's rounding method is wrong for negative numbers
2.4 rounded is 2 (olliej's method gets this right)
−2.4 rounded is −2 (olliej's method returns -1)
Here's an alternative
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))
You could of course use a rounding function from math.h
// Converting String in to Double
double doubleValue = [yourString doubleValue];
// Converting Double in to String
NSString *yourString = [NSString stringWithFormat:#"%.20f", doubleValue];
// .20f takes the value up to 20 position after decimal
// Converting double to int
int intValue = (int) doubleValue;
or
int intValue = [yourString intValue];
For conversion from a number to a string, how about using the new literals syntax (XCode >= 4.4), its a little more compact.
int myInt = (int)round( [#"1.6" floatValue] );
NSString* myString = [#(myInt) description];
(Boxes it up as a NSNumber and converts to a string using the NSObjects' description method)
For rounding, you should probably use the C functions defined in math.h.
int roundedX = round(x);
Hold down Option and double click on round in Xcode and it will show you the man page with various functions for rounding different types.
This is the easiest way I know of:
float myFloat = 5.3;
NSInteger myInt = (NSInteger)myFloat;
from this example here, you can see the the conversions both ways:
NSString *str=#"5678901234567890";
long long verylong;
NSRange range;
range.length = 15;
range.location = 0;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanLongLong:&verylong];
NSLog(#"long long value %lld",verylong);
convert text entered in textfield to integer
double mydouble=[_myTextfield.text doubleValue];
rounding to the nearest double
mydouble=(round(mydouble));
rounding to the nearest int(considering only positive values)
int myint=(int)(mydouble);
converting from double to string
myLabel.text=[NSString stringWithFormat:#"%f",mydouble];
or
NSString *mystring=[NSString stringWithFormat:#"%f",mydouble];
converting from int to string
myLabel.text=[NSString stringWithFormat:#"%d",myint];
or
NSString *mystring=[NSString stringWithFormat:#"%f",mydouble];
I ended up using this handy macro:
#define STRING(value) [#(value) stringValue]