Extracting numeric value from UItextfield post-NSFormatter decimal conversion - objective-c

I am having the user enter into a textfield a number, say 900000. It is then formatted for decimal and shows on the screen as 900,000. When I try to extract the numeric value from the formatted textfield, the number returned is 900. Suggestions?

You could prior to converting to a number delete the , with
stringByReplacingOccurrencesOfString:#"," withString:#""
But this will not work in much of the world where a ' is used as the decimal point.
Better to use an NSNumberFormatter:
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *numberObject = [numberFormatter numberFromString:#"900,000"];
NSLog(#"numberObject: %#", numberObject);
int numberInt = [numberObject intValue];
NSLog(#"numberInt: %d", numberInt);
Output:
numberObject: 900000
numberInt: 900000

have you tried removing the comma?
str = [str stringByReplacingOccurrencesOfString:#","
withString:#""];
int value = [str intValue];

Related

Not seeing the decimal from a string when converted into NSDecimalNumer

I can't see to find the answer to this, but I have a string that has a decimal point in it, and when I try to convert it to a NSDecimalNumber I only get back the whole number, not the decimal or what would come after it. This is what I am trying:
someText.text = #"200.00";
tempAmountOwed = [NSDecimalNumber decimalNumberWithString:someText.text]; // gives me back 200
I can't seem to figure out if the decimalNumberWithString method is stripping out my decimal and ignoring what comes after it.
Thanks for the help!
You can use the method decimalNumberWithString: locale: method.
for eg:-
The code:
NSLog(#"%#", [NSDecimalNumber decimalNumberWithString:#"200.00"]);
NSLog(#"%#", [NSDecimalNumber decimalNumberWithString:#"200.00" locale:NSLocale.currentLocale]);
Gives following log:
200
200.00
Hope this Helps!
That's perfectly normal. If your decimal String doesn't contain fractions it won't print them. If you want to print them you can use a NSNumberFormatter or convert it to a float and print it with %.2f to do so:
NSString *text = #"200.00";
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:text];
NSLog(#"%#", number); //this will print "200"
//solution #1
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
numberFormatter.minimumFractionDigits = 2;
NSLog(#"%#", [numberFormatter stringFromNumber:number]); //this will print "200.00"
//solution #2
CGFloat number = [text floatValue];
NSLog(#"%.2f", number); //this will print "200.00"

How can i decrement an NSString's Value from a UITextField Value input by user?

I am trying to decrement an NSString value which may hold a saved amount (Ex: 560.00)
and when the user enters a value into a UITextField Object, say, 50.00, the NSString holding the value of 560.00 decrements to 510.00 from 50.00 and still holds the Value without deleting the entire value or setting it to 50.00 ?
Use NSNumberFormatter to parse NSString value to NSNumber and reverse. In addition it will allow you to handle user's locale.
Basic example:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *numberFromString = [numberFormatter numberFromString:userInput];
double valueAfterDecrement = [numberFromString doubleValue] - delta;
NSString *stringFromNumber = [numberFormatter stringFromNumber:#(valueAfterDecrement)];
here a basic example.
NSString *string = #"50.22";
float number=[string floatValue];
NSLog(#"%f",number);
NSString *string1 = #"560.22";
float number1=[string1 floatValue];
NSLog(#"%f",number1);
float ans = number1 - number;
NSLog(#"%f",ans);
NSString * finalStr = [NSString stringWithFormat:#"%f",ans];

How do I combine a char with an int in an NSTextField in Objective-C?

For example, I have the following code, where lblPercent is an NSTextField:
double Progress = progress( Points);
[lblPercent setIntValue:(Progress)];
I set it as integer value so it tosses out the decimal, since for some reason the NSProgressIndicator forces me to use a double. Anyway, in the label adjacent to the progress bar, I want it see the number x% with the percent sign next to it.
I tried standard concatenation techniques but no dice.
You should use an NSNumberFormatter with the percent style
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterPercentStyle];
// Any other format settings you want
NSString* formattedNumber = [formatter stringFromNumber: [NSNumber numberWithDouble: progress]];
try
[lblPercent setText:[NSString stringWithFormat:#"%d%%",[Progress intValue]]];
NSMutableString *value = lblPercent.text;
[value appendString:#"%"];
[lblPercent setText:value];
You can use unicode characters to get the percent sign.
i.e.
double value;
myLabel.text = [NSString stringWithFormat:#"%d\u0025", value ]
u0025 is the unicode character for 'percent sign'
NSInteger percentageProgress = (NSInteger) (Progress * 100);
[lblPercent setText:[NSString stringWithFormat:#"%d%%", percentageProgress]];
NSString *string = [NSString stringWithFormat:#"%.0f%#",Progress, #"%"];
[lblPercent setStringValue:string];
This seems to had worked for me doing it the way I had done it...

How to convert NSString to NSInteger in iPhone application?

NSString * addString=[arrayyyy componentsJoinedByString:#","];
NSLog(#"add string is: %#",addString);// result is: 45,1
Now I want to convert above string into integer.
I have tried this:
NSInteger myInt=[addString intValue];
//NSLog(#"myInt is: %d",myInt);// result is: 45
If you expected 45.1 then there are two things wrong :
45.1 is not an integer. You would have to use floatValue to read the value.
45,1 (notice the comma) is not a valid float number. While 45,1 is valid in some locale (i.e. in french its 1 000,25 instead of 1,000.25) you would have to convert the string with an NSNumberFormatter before reading the floatValue.
.
// Can't compile and verify this right now, so please bear with me.
NSString *str = #"45,1";
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"] autorelease]; // lets say French from France
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setLocale:locale];
float value = [[formatter numberFromString:str] floatValue]; // value = 45.1
Try out NSExpression which works with mathematical symbols too (i.e. +, -, /, *):
NSNumber *numberValue = [[NSExpression expressionWithFormat:inputString] expressionValueWithObject:nil context:nil];
// do something with numberValue
From reading the question a lot, I think I may understand what you want.
The starting point seems to be:
NSLog(#"add string is: %#",addString);// result is: 45,1
And the current ending point is:
NSLog(#"myInt is: %d",myInt);// result is: 45
But it seems that you still want to print out 45,1
My guess on this is that you have an array of 2 strings [#"45",#"1"] called arrayyyy and you want to print out both values as integers. If this is so then what I think you want is:
NSInteger myInt1 = [[arrayyyy objectAtIndex:0] intValue];
NSInteger myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(#"add string is: %d,%d",myInt1,myInt2);
Note This will crash horribly with an NSRangeException if there are not at least two strings in the array. So at the very least you should do:
NSInteger myInt1 = -1;
NSInteger myInt2 = -1;
if ([arrayyyy length] >0) myInt1 = [[arrayyyy objectAtIndex:0] intValue];
if ([arrayyyy length] >1) myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(#"add string is: %d,%d",myInt1,myInt2);
But even this is bad as it assumes that the guard value of -1 will not be present in the actual data.

Convert a number with a comma into a number with point. (Cocoa Touch)

I'm trying to code a calculator with XCode, but then I saw that Numbers with a comma are just cutted of after the comma.
I am getting the Numbers with this code out of the textfield.
-(IBAction)Additionbutton:(id)sender{
NSString *firstString = field1.text;
NSString *secondString = field2.text;
float num1;
float num2;
float output;
num1 = [firstString floatValue];
num2 = [secondString floatValue];
output = num1 + num2
Solutionfield.text = [ [NSString alloc] initWithFormat:#"%.2f",output] ;
Is it possible to set the calculation that way, that it is able to handle points AND commas or do I have to convert them and if so, how can I do this?
Thank you :)
Maybe you should use: NSNumberFormatter
How to convert an NSString into an NSNumber
I have experienced the same thing this morning, following solution worked for me:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *aNumber = [NSNumber numberWithFloat:[[yourFloatAsString stringByReplacingOccurrencesOfString:#"," withString:#"" ] floatValue]];
[numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];
NSString *formattedNumber;
formattedNumber = [numberFormatter stringFromNumber:aNumber];
NSLog(#"formattedNumber: %#", formattedNumber);
Obviously not the most efficient solution possible. It works, but if you have some time I strongly suggest you to have a look at NSNumberFormatter Class Reference