Conver text in numbers to digits - objective-c

I have an application for the Apple Watch that uses Siri for getting input from the user. I need to get the response in numbers when the user speak, but the below method only returns numbers as strings, e.g. "five" instead of "5". Any help on how I can achieve this?
presentTextInputControllerWithSuggestions

NSNumberFormatter can convert from text to numbers.
Example
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
NSLog(#"%#", [formatter numberFromString:#"thirty-four"]);
NSLog(#"%#", [formatter numberFromString:#"three point five"]);
formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:#{NSLocaleLanguageCode: #"es"}]];
NSLog(#"%#", [formatter numberFromString:#"ocho"]);

Try this.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
NSNumber *num = [formatter numberFromString:#"yourWord"];
int value = [num intValue];

Related

How to send NSDate with a certain date format as NSNumber? i.e convert NSDate to NSDate with another format?

I have two user selected dates: startDate and endDate. They are NSDate instances and I have to send them as parameters as NSNumbers. How can I convert them to NSNumber with seconds?
Use below code :
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// set format however you want
[formatter setDateFormat:#"ddMMyyyy"];
NSDate *date = [NSDate date];
NSString *string = [formatter stringFromDate:date];
NSNumber *num1 = #([string intValue]);
NSLog(#"%#",num1);
1) First, get the date in MM/dd/yyyy format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
2) get string date and remove '/' from it:
NString *string = [formatter stringFromDate:date];
NString *finalStr = [string stringByReplacingOccurrencesOfString:#"/" withString:#""];
3) Use NSNumberFormatter from converting NSString to NSNumber:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *myNumber = [f numberFromString: finalStr];
Hope, this is what you want!
If you mean a timestamp format
NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];
How to convert NSDate into Unix timestamp in Objective C/iPhone?
Thanks,

How to convert numbers into indicative string

I just want to know how can I can convert the numbers 1, 2 or 3 into First, second or Third ?
Any help would be appreciated.
Try this,
- (NSString *)getOrdinalTextFor:(NSInteger)number {
NSNumber *numberValue = [NSNumber numberWithInteger:number];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterOrdinalStyle];
return [numberFormatter stringFromNumber:numberValue];
}
See this one may be it helps you,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: 3]];
NSLog(#"%#",numberString);
enter link description hereDownload Formatter kit from GitHub
TTTOrdinalNumberFormatter *ordinalNumberFormatter = [[TTTOrdinalNumberFormatter alloc] init];
[ordinalNumberFormatter setGrammaticalGender:TTTOrdinalNumberFormatterMaleGender];
NSNumber *number = [NSNumber numberWithInteger:2];
NSString *str = [ordinalNumberFormatter stringFromNumber:number];
NSLog(#"%#", str);
Read up this link, its very helpful: Number Formatting

How to avoid grouping separator in NSNumberFormater until 5 digits

I have an application that needs to display metric units in millimeters and based on the research that I have done (e.g. http://physics.nist.gov/cuu/pdf/sp811.pdf), it is recommended that you don't add grouping separators until you have 5 digits. Is this possible with NSNumberFormatter? I know I could test the value and enable/disable grouping based on its size but I would rather use the appropriate configuration for the NSNumberFormatter if it exists. (BTW, I am on Mac OS X) So for example:
3456 should be shown as 3456 mm
10234 should be shown as 10,234 mm (assuming comma is the grouping separator)
I have read the Apple docs and didn't see anything. I experimented with different searches to find the answer on Stackoverflow but couldn't find a similar question.
Here is how I am currently setting up my NSNumberFormatter:
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
long long d = fLenInMMs;
if (d>9999) {
NSNumberFormatter *numberFormatter=nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
NSLog(#"%#", numberStr);
}
else {
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [NSString stringWithFormat:#"%# mm",numberInMM];
}
EDIT
You can also subclass NSNumberFormatter and implement
- (NSString *)stringFromNumber:(NSNumber *)number
{
NSNumber *nm = [NSNumber numberWithInt:9999];
if (NSOrderedAscending==[number compare:nm]) {
return [NSString stringWithFormat:#"%# mm",number];
}
return [super stringFromNumber:number];
}

rfc 3339 to dd.mm.yyyy format

I have a string like that:
NSString *string = #"2012-06-14T00:00:00+03:00";
I tried this and get "2012-06-14 21:00:00 GMT". But i need "14.06.2012". Is it possible?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:string range:nil error:&error];
You'll need to create a second NSDateFormatter for the desired format (in this case "dd.MM.yyyy"), and use its stringFromDate: methods on your date object to get a string of the desired format. Try adding this code after your existing code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"dd.MM.yyyy";
NSString *result = [formatter stringFromDate:date];

How to use NSNumberFormatter to display currency?

I want to format a number with NSNumberFormatter to be displayed as currency like this:
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
NSNumber *myNumber = [NSNumber numberWithDouble:[#"0.058" doubleValue]];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
This gives me 0,06 €. But I want it to be 5,8 ct. Any ideas?
You can configure NSNumberFormatter like that :
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numFormatter setCurrencySymbol:#"ct"];
[numFormatter setMultiplier:100];
NSNumber *myNumber = [NSNumber numberWithDouble:[#"0.058" doubleValue]];