This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: How to format string as $ Price
I have an App showing prices.
The prices are displayed that way:
[self makePrice];
itemPrice.text = [NSString stringWithFormat:#"%i",tempPrice];
itemPrice.textColor = [UIColor whiteColor];
As a result big values are shown as: $ 12123041
I need it to be $ 12,123,041
How can I do it?
Thanks. Appreciate your help
Tim
hi you can do like this way:-
int tempPrice=123456;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *yourNumber = [NSNumber numberWithInt:tempPrice];
NSString *str = [formatter stringFromNumber:yourNumber];
str = [NSString stringWithFormat:#"%#",str];
NSLog(#"my corrunsy === %#",str);
output is
hiiii === $123,456.00
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *num = [NSNumber numberWithInt:tempPrice];
NSString *str = [formatter stringFromNumber:num];
itemPrice.text = [NSString stringWithFormat:#"%#",str];
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 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]];
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I create the function to convert the Currency to Word Format but my problem is that when put the textfeild value 100000 i need Indian Format one lakh,but it give me one hundred thousand so any solution for that.
NSString *str1 = txtAmount.text;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString *localeStr = [appDel.countryCodeDict valueForKey:appDel.selectedCountry];
if ([appDel.selectedCountry isEqualToString:#"France"]) {
localeStr = #"fr";
}
if ([appDel.selectedCountry isEqualToString:#"Germany"]) {
localeStr = #"de";
}
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeStr];
[formatter setLocale:usLocale];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSArray *valueArr=[str1 componentsSeparatedByString:#"."];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString *firstStr = [valueArr objectAtIndex:0];
NSString *seconfStr = [valueArr lastObject];
if (valueArr.count==2 && ![seconfStr isEqualToString:#""]) {
double firstAmt = [firstStr doubleValue];
NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:firstAmt]];
double secondAmt = [seconfStr doubleValue];
NSString *convertStr1 = [formatter stringFromNumber:[NSNumber numberWithDouble:secondAmt]];
NSString *finalStr = [convertStr stringByAppendingFormat:#" %# %# %#",appDel.firstCurrencystr,convertStr1,appDel.secondCurrencystr];
NSLog(#"%#",finalStr);
NSString *firstCapChar = [[finalStr substringToIndex:1] capitalizedString];
tempStr = [finalStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
}else{
double firstAmt = [firstStr doubleValue];
NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:firstAmt]];
convertStr = [convertStr stringByAppendingFormat:#" %#",appDel.firstCurrencystr];
NSString *firstCapChar = [[convertStr substringToIndex:1] capitalizedString];
tempStr = [convertStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
}
appDel.lblAmountView.lblAmount.text = tempStr;
Any idea of that.
Dont' know what you have coded but if you want currency to be in indian style check out this code:-
NSLocale *indiaLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"] autorelease];
double currency = 1000000000.00;
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:indiaLocale];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
NSString *str2 = [NSString stringWithFormat:#"Converted:%#",numberAsString];
NSLog(#"String:%#",str2);
at last i convert the hole currency in lakh and crore format of india:
-(NSString *)stringfromNumberIndia:(NSString *)str_word
{
NSString *tempStr=#"";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
[formatter setLocale:usLocale];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
if ([str_word length]>5) {
NSString* str_convert = [str_word substringFromIndex:[str_word length]-5];
NSString* str_convert1 = [str_word substringToIndex:[str_word length]-5];
NSLog(#"str :%#",str_convert);
NSLog(#"str1 :%#",str_convert1);
tempStr = [[self stringfromNumberIndia:str_convert] stringByAppendingString:tempStr];
int l=[str_convert1 length];
for (int i=0; i<l;) {
NSRange range;
if (l>2 && i == 0) {
range=NSMakeRange(l-i-2, 2);
}
else{
range=NSMakeRange(0, l-i);
}
NSString *str_first=[str_convert1 substringWithRange:range];
if ([str_first intValue] == 0) {
if (i == 0)
i+=2;
else
break;
continue;
}
NSLog(#"str_first:%#",str_first);
if (i== 0) {
str_first=[formatter stringFromNumber:[NSNumber numberWithDouble:[str_first doubleValue]]];
str_first = [str_first stringByAppendingFormat:#" lakh "];
tempStr = [str_first stringByAppendingString:tempStr];
}
else {
str_first =[self stringfromNumberIndia:str_first];
str_first = [str_first stringByAppendingFormat:#" crore "];
tempStr = [str_first stringByAppendingString:tempStr];
break;
}
i+=2;
}
}
else if ([str_word intValue] !=0){
double Amt = [str_word doubleValue];
NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:Amt]];
NSString *firstCapChar = [convertStr substringToIndex:1];
tempStr = [convertStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
}
return tempStr;
}
Thanks.
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];