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
Related
It can be a duplicate question,but whatever i found it didn't help me.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:#","];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:#"."];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
NSString *theString = [numberFormatter stringFromNumber:num];
I'm using this for thousands string conversion.But if i pass values like 5123.00 it converts it to 5,123,but i need all the decimal values(like 5,123.00) after decimal point, even it is zero.How to do this,please provide some solution?
Add
[numberFormatter setMinimumFractionDigits:2];
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
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];
}
I am trying to format a NSNumber containing a value of 0.305 as "30.5%".
However, my following code does not work:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
How can I specify that the number should be formatted as percentage with one decimal place?
This works for me:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Edited for the actual correct answer:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Try
float percent = 0.305;
NSString *formattedPercentText = [NSString stringWithFormat:#"%.1f%%", percent * 100];
I'm using also setMaximum
float precent = 3.5;
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setMaximumFractionDigits:1];
return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:precent]];
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]];