How can I check if which side the currency symbol is at? For example, in the United States, the symbol would be like this: "$56.58", but in France it would be like this: "56.58€". So how would I be able to detect if it's on the right or left side?
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setLocale:[NSLocale currentLocale]];
If you just want to format a number as currency, set the formatter's numberStyle to NSNumberFormatterCurrencyStyle and then use its stringFromNumber: method.
If, for some reason, you really want to know the position of the currency symbol in the format for the formatter's locale, you can ask the formatter for its positiveFormat and look for the character ¤ (U+00A4 CURRENCY SIGN).
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterCurrencyStyle;
f.locale = [NSLocale localeWithLocaleIdentifier:#"en-US"];
NSLog(#"%# format=[%#] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
(unsigned long)[f.positiveFormat rangeOfString:#"\u00a4"].location);
f.locale = [NSLocale localeWithLocaleIdentifier:#"fr-FR"];
NSLog(#"%# format=[%#] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
(unsigned long)[f.positiveFormat rangeOfString:#"\u00a4"].location);
f.locale = [NSLocale localeWithLocaleIdentifier:#"fa-IR"];
NSLog(#"%# format=[%#] ¤-index=%lu", f.locale.localeIdentifier, f.positiveFormat,
(unsigned long)[f.positiveFormat rangeOfString:#"\u00a4"].location);
Result:
2015-06-10 21:27:09.807 commandline[88239:3716428] en-US format=[¤#,##0.00] ¤-index=0
2015-06-10 21:27:09.808 commandline[88239:3716428] fr-FR format=[#,##0.00 ¤] ¤-index=9
2015-06-10 21:27:09.808 commandline[88239:3716428] fa-IR format=[¤#,##0] ¤-index=1
Note that in the fa-IR case, the symbol is not the first or last character in the format string. The first character (at index zero) is invisible. It's U+200E LEFT-TO-RIGHT MARK.
Related
I have some amount that i want to Display in local Style.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
numberFormatter.locale = [NSLocale currentLocale];
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
numberFormatter.usesGroupingSeparator = YES;
NSString* amountText = [NSString stringWithFormat:#"ab %#€",[numberFormatter stringFromNumber:someDoubleNumber]];
Problem is that this is also displaying the currency and that is something that i don't want. Basically what i am looking for is displaying that decimal should be according to local Style with 2 numbers after decimal for example 0.00 or in germany 0,00 with my code i am achieveing this requirement but i am also getting the additionally the currency symbol which i don't want.
Anybody still looking for an answer i achieved it by
[numberFormatter setCurrencySymbol:#""];
for number, after formatting, I hope it show as $050, below code does not work. But If I use NSNumberFormatterDecimalStyle, it can show 050. Do you know why?
NSNumberFormatter *f = [[NSNumberFormatter alloc] init] ;
f.numberStyle = NSNumberFormatterNoStyle;
NSNumber *myNumber;
NSString *myString;
myNumber = [f numberFromString:#"50"]; // Note that the extra zeros are useless
[f setNumberStyle:NSNumberFormatterCurrencyAccountingStyle];
[f setFormatWidth:3];
//[f setPaddingPosition:NSNumberFormatterPadAfterPrefix];
[f setPaddingCharacter:#"0"];
myString = [f stringFromNumber:myNumber];
NSLog(#"myString: %#",myString);
The boring answer is that US currency style (and Australian and Canadian, as far as I know) never uses leading zeroes, unless the amount is zero dollars and some cents (e.g. "$0.43"). The NSNumberFormatterDecimalStyle, on the other hand, can use padding zeroes.
I have number 36381129. I need number 36.381,129
I tried this code, but it doesn't work.
int number = 36381129;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber: [NSNumber numberWithInt:number]];
I give this number.
36.381.129,00 $
I think this is BRAZILIAN REAL CURRENCY Format. You have to call this method with your price in float value, and this method returns your string into your format. Like if we pass 123456789, then it will return 123,456,789.00.
//Convert Price to Your Price Format
+(NSString*)convertFormat:(float)value{
NSString * convertedString = [NSString stringWithFormat:#"%.2f", value];
NSString * leftPart;
NSString * rightPart;
if (([convertedString rangeOfString:#"."].location != NSNotFound)) {
rightPart = [[convertedString componentsSeparatedByString:#"."] objectAtIndex:1];
leftPart = [[convertedString componentsSeparatedByString:#"."] objectAtIndex:0];
}
//NSLog(#"%d",[leftPart length]);
NSMutableString *mu = [NSMutableString stringWithString:leftPart];
if ([mu length] > 3) {
[mu insertString:#"." atIndex:[mu length] - 3];
//NSLog(#"String is %# and length is %d", mu, [mu length]);
}
for (int i=7; i<[mu length]; i=i+4) {
[mu insertString:#"." atIndex:[mu length] - i];
//NSLog(#"%d",mu.length);
}
convertedString = [[mu stringByAppendingString:#","] stringByAppendingString:rightPart];
return convertedString;
}
For more details, refer this blog.
Hope, this is what you're looking for. Any concern get back to me.
Welcome to SO. Your question is pretty vague.
Currency formats depend on the user's locale. It's generally better to either use the default locale of the device, or set a locale, and then let the currency formatter create that string that's appropriate for that locale.
If you set up a hard-coded currency format then it will be wrong for some users. (For example in the US we use a "." as a decimal separator and commas as a grouping symbol. In most of Europe they use a comma as a decimal separator and the period as a grouping symbol. Some countries put the currency symbol at the end of a currency amount, and others put it at the beginning.)
You can use this code:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];
and use it this way:
NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];
This is a generic solution and will work for any country according to their grouping separator
Taken from: https://stackoverflow.com/a/5407103/2082569
How do I know if a language NSLocale uses spaces for delimiting words in a sentence (like English or other roman languages) or not (like Japanese)?
I expected to find this information under NSLocale Component Keys … no. Any idea? Do I really need to set my own dictionary for this. I'd appreciate any advice or related resource.
You can use NSLocal Components keys' NSLocaleExemplarCharacterSet to get the set of character in that language and then see if space is part of that language character set
NSLocale *jpLocale = [[NSLocal alloc] initWithLocalDentifier: #"ja_JP"];
NSCharacterSet *jpCharSet = (NSCharacterSet *)[jpLocale objectForKey: NSLocaleExemplarCharacterSet];
[jpCharSet characterIsMember: ' '] ? NSLog("Yeah it uses space"); : NSLog("Nope");
I still haven't found a great solution. As a workaround I am checking for spaces in fullStyle strings from a random date using the NSDateFormatter
NSDate* exampleDate = [NSDate dateWithTimeIntervalSince1970:0];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.locale = [NSLocale currentLocale];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
NSString *testString = [dateFormatter stringFromDate:exampleDate];
BOOL localeLikelyUsesSpaceAsDelimitters = [testString rangeOfString:#" "].location == NSNotFound;
Better ideas?
I would like to make a string with stringWithFormat from a double value, without the unnecessary zero at the end.
Examples:
[NSString stringWithFormat:#"%.8f",2.344383933];
[NSString stringWithFormat:#"%.8f",2.0];
expected results:
2.344383933
2
Which is the correct format ?
Thank you.
Use NSNumberFormatter
[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",0]]
Sample:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSLog(#"1: %#",[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",2.344383933]]);
NSLog(#"2: %#",[numberFormatter numberFromString:[NSString stringWithFormat:#"%.8f",2.0]]);
Results:
1: 2.344383933
2: 2
There is a dedicated class for number formatting, NSNumberFormatter:
let formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 8
print("\(formatter.stringFromNumber(2.344383933))")
print("\(formatter.stringFromNumber(2.0))")
NSNumberFormatter will also bring localization (decimal points, grouping separators).