I have a StopWatch in my app. The time is displayed on a label with format: mm:ss.SSS as an NSString. I need to convert it to NSNumber. I searched Google many times, but couldn't find the answer.
Thanks!
The following gives you the date/time as a number in seconds since the reference date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm:ss"];
NSDate *date = [formatter dateFromString:textField.text];
NSNumber *dateAsNumber = [NSNumber numberWithFloat:[date timeIntervalSince1970]];
try the code below :
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:#"42"];
[f release];
but change #"42" with your label_name.text
This is the way you can convert an NSString into NSNumber.
NSString *timeFormat = #"Your Time";
NSNumber *time = [NSNumber numberWithInt:[timeFormat integerValue]];
Related
I have a string like "1.23,45", (Germany/French style or other style).
How can I remove the format to normal string or to nsnumber?
I only know convert from number to string.:(
thanks.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencySymbol:#""];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:locale];
NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithFloat:[aPrice floatValue]]];
[locale release];
[formatter release];
NSLog(#"%#", formattedOutput];
Further converting formattedString to number
NSNumber myNumber = [formattedOutput intValue]; //replace intValue by floatValue to get decimal output
You can convert a NSString to a NSNumber using a NSNumberFormatter, but you have to tell it which locale that you are using for your strings (German, in this case):
// Create a number formatter that can convert from/to German currency:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
[formatter setLenient:YES];
// Use the number formatter to create a NSNumber:
NSNumber *number = [formatter numberFromString:#"1.23,45"];
// Return the number formatter to the current user's locale:
formatter.locale = [NSLocale currentLocale];
// Finally, create a string from the NSNumber formatted in the user's locale:
NSString *formattedString = [formatter stringFromNumber:number];
NSNumber * myNumber = [NSNumber numberWithInt:[#"1.23,45" intValue]];
or
NSNumber * myNumber = [NSNumber numberWithFloat:[#"1.23,45" floatValue]];
Note: better to trim the comma (,) symbol from string
You can use the below methods
+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
It sounds like you need to do two things:
1) break the string into an arrays of substrings based upon comma separation
NSString *startString = #"1.23,45";
NSArray *subStringArray= [colorStr componentsSeparatedByString:#","];
2) convert each substring to a number
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *subString = [subStringArray objectAtIndex:0];
NSNumber *thisNumber = [formatter numberFromString:subString];
NSLog (#"thisNumber: %#", thisNumber);
I need to concatenate a NSString with the NSString output of NSDateFormatter. My code is below.
Please check where I am going wrong. I have to concatenate DEST PATH with datestring.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
result =[result stringByAppendingString:DEST_PATH];
result=[result stringByAppendingString:dateString];
It appears as though you never instantiate the NSString result. I would change the above to the following:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
NSString* result=[DEST_PATH stringByAppendingPathComponent:dateString];
If you use the stringByAppendingPathComponent method it will automatically add the slash for you.
Also, using all capital letters for identifiers usually denotes a c style propocessor macro constant, so using it as a variable may confuse some people.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
NSString *resultStr=[NSString stringWithFormat:#"%#%#",DEST_PATH,dateString];
Hope this helps you.
How can I convert NSNumber "1234567 " to "12,34,567"?
I have used the NSNumberFormatter for this,but couldn't get the desired result.
I am doing like this
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:1234567]];
NSLog(#":%#",formattedString);
Output: 1,234,567
Desired Output: 12,34,567
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:#"##,##,###"];
NSString *formattedNumberString = [numberFormatter
stringFromNumber:[NSNumber numberWithFloat:1234567]];
NSLog(#"formattedNumberString: %#", formattedNumberString);
[numberFormatter release];
NSNumber *number = [NSNumber numberWithInteger:1234567];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(#"%#", [formatter stringFromNumber:number]);
[formatter release];
I have a NSString that has the following number 0.871 and I'm trying to convert this to display like so 87.1%
I tried to use NSNumberFormatter to get that result but I can't get it to display 3 digits. What's the best way to achieve this result? Thanks.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSNumber *number = [formatter numberFromString:inputString];
[formatter release];
NSString * str = #"0.863";
NSString * newStr = [NSString stringWithFormat:#"%.1f%%", [str floatValue]*100];
If you're trying to convert a string to a number, then format the number:
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:inputString];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSString *formattedString = [formatter stringFromNumber:number];
[formatter release];
If you just want to turn "0.871" into the number 87.1:
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:inputString];
NSDecimalNumber *percentNumber = [number decimalNumberByMultiplyingByPowerOf10:2];
Just use setMinimumFractionDigits:
NSNumber *myNumber = [NSNumber numberWithFloat:0.871];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
[formatter setMinimumFractionDigits:1];
NSLog(#"%#", [formatter stringFromNumber:myNumber]); // 87.1%
this also ensures that percentages are formatted appropriately for the locale.
I am converting an NSNumber to an NSString assuming in this example that the selected key in "theindex" is 1000000
NSNumber *firstNumber = [tempDict objectForKey:#"theindex"];
NSString *convertNumber = [firstNumber stringValue];
Returning the NSString "1000000"
I would like the string's value to be "1,000,000".
I am not concerned with localization, but understand from other questions that NSNumberFormatter should be implemented. I am not sure how to accomplish this?
Here is an example using a standard formatting style of NSNumberFormatter:
NSNumber *firstNumber = [NSNumber numberWithInt:123456789];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
NSLog(#"value : %#", convertNumber);
Other examples you can reference at:
http://iosdevelopertips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html
NSNumber *firstNumber = [[NSNumber alloc] initWithInt:1000000] ;
NSNumberFormatter *format=[[NSNumberFormatter alloc] init];
[format setGroupingSize:3];
[format setCurrencyGroupingSeparator:#","];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [format stringFromNumber:firstNumber];