How to use NSNumberFormatter to display currency? - objective-c

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

Related

Conver text in numbers to digits

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

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

NSNumberFormatter conversion remove the decimal for small values

Our quality-assurance team detect a defect. When they write 0.095 or 0,095 in a currency field, the NSNumberFormatter converts the content to 95.
This is the code we're using:
+ (NSNumber *) getNumberWithDecimalFromString:(NSString*) theSource
{
if ([TSCommons isEmpty:theSource])
return [[NSNumber alloc] initWithInt:0];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
if ([theSource rangeOfString:#","].location != NSNotFound) {
[formatter setDecimalSeparator:#","];
[formatter setCurrencyDecimalSeparator:#"."];
} else if ([theSource rangeOfString:#"."].location != NSNotFound) {
[formatter setDecimalSeparator:#"."];
[formatter setCurrencyDecimalSeparator:#"."];
}
NSNumber *tmp = [formatter numberFromString:theSource];
[formatter release];
return [tmp retain];
}
The locale is es_ES
Any idea about this unexpected behavior?
To introduce more information about this problem, because come back again after the last XCode update I prepared a test
- (void) testNumberFormatter
{
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,1"] doubleValue]);
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,12342"] doubleValue]);
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,12"] floatValue]);
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,123"] floatValue]);
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,1234"] floatValue]);
NSLog(#"%f",[[TSCommons getNumberWithDecimalFromString:#"41,1234234"] doubleValue]);
}
The result on the Console is the following:
41.100000
41.123420
41.119999
41123.000000
41.123402
41.123423
Thanks!!
NSString* theSource = #"0.095";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
if ([theSource rangeOfString:#","].location != NSNotFound) {
[formatter setDecimalSeparator:#","];
[formatter setCurrencyDecimalSeparator:#"."];
} else if ([theSource rangeOfString:#"."].location != NSNotFound) {
[formatter setDecimalSeparator:#"."];
[formatter setCurrencyDecimalSeparator:#"."];
}
NSNumber *tmp = [formatter numberFromString:theSource];
NSLog(#"get Number %# - %lf %lf", tmp, [theSource doubleValue], [tmp floatValue]);
// output get Number 0.095 - 0.095000 0.095000

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];
}

How to truncate .000 zeros from double with NSNumberFormatter

Ok so if I have a double to begin with how do I limit it or truncate digits to 1dp since I know they're not needed to be viewed
double a = 1.6;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:a]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setAlwaysShowsDecimalSeparator:YES];
NSLog(#"a raw: %f", a); //outputs 1.600000
myLabel.text = [NSString stringWithFormat:#"%#",numberAsString];
NSLog(#"Formatted a: %#", numberAsString); //outputs 2
[numberFormatter release];
So the problem is it rounds the value and leaves no decimal place, I'm trying to get it to output just 1.6 for myLabel.
I am not sure but you can try with NSString *formatted = [NSString stringWithFormat:#"%.1f", a];, then use the formatted as the label text