NSLocale *locale = [NSLocale currentLocale];
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setLocale:locale];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setCurrencySymbol:[countriesCurrency objectForKey:currency]];
[currencyStyle setRoundingIncrement:[NSNumber numberWithFloat:0.01]];
Since 2012 code like below returned "0.70 €" and that was great as it's exactly what is expected.
po [currencyStyle stringFromNumber:[NSNumber numberWithFloat:0.70]]
BUT since iOS 13 (app build with xCode 10.3, just users with iOS 13 on their iPhones installed app from the AppStore. Same for build throw xCode 11 on iOS 13.0 and 13.1 beta) exactly same code returns "0,69999998435378074 €"
After quick midnight investigations found that if add
currencyStyle.usesSignificantDigits = YES;
then result will be "0.7 €" - much better but still not exactly what was expected ("0.70 €").
Any ideas how to make setRoundingIncrement great again?
[UPDATE]:
Rewriting the same code to swift solve the problem but this sounds like temp solution in the case all the rest app is in Obj-C.
Related
This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 7 years ago.
I'm have a problem with date formatting in iOS 9, it works ok with iOS 8 and also it works when I'm testing it in simulator with iOS 9, but when a test it on real device with iOS 9 I'm getting null. Below is code that I'm using
NSLog(#"String Date: '%#'", stringDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:stringDate];
NSLog(#"Date: %#", date);
In log I'm getting:
String Date: '8/29/2015 4:13:39 PM'
Date: (null)
Also if I use uppercase h (H or HH) I'm always getting that hour is 10.
Try setting locale on your date formatter.
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
From Apple Documentation:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
EDIT: Swift Version
dateFormat.locale = NSLocale(localeIdentifier: "en_US_POSIX")
An NSDateFormatter instance can have it's date and time formats set with:
[formatter setDateFormat: formatString];
Or you can have your format string localised with:
[formatter setDateFormat:
[NSDateFormatter
dateFormatFromTemplate: formatStringWithPatterns
options: 0
locale: [NSLocale currentLocale]];
But where's a handy table of all the codes that can be used in format strings, giving examples of each's output?
I've just pulled this out of Apple's docs because I can never find it when I need it. Hopefully S.O. will be better linked!
For the latest versions of iOS and OS X, the answer is:
Formatters in OS X v10.8+ and iOS 6.0+, use version tr35-25.
For prior versions, you'll need to look more closely:
Formatters in iOS 5.0-5.1 use version tr35-19.
Formatters in OS X v10.7 and iOS 4.3 use version tr35-17.
Formatters in iOS 4.0-4.2 use version tr35-15.
Formatters in iOS 3.2 use version tr35-12.
Formatters in OS X v10.6 and iOS 3.0-3.1 use version tr35-10.
Simply, I have a positive integer [9, 393, 3, 993], and I would like to localize it to a certain language [٩, ٣٩٣ ,٣ ,٩٣٩].
If I use NSNumberFormatter, it will localize the number according to the user's locale. However, I want to override that and choose any locale to translate the number to.
I tried the following, did not work:
// user locale is #"en"
NSNumberFormatter* formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ar"]];
[formatter setMinimumIntegerDigits:padding];
return [formatter stringFromNumber:#(num)];
The returned string is in English.
Please note that I have a very similar code snippet for NSDateFormatter, but it works as expected. The NSDateFormatter object respects the set locale.
It seems I came across a very special case where the locale of the app would just freak out.
I am changing the default locale of the app by using something like this:
[[NSUserDefaults standardUserDefaults] setObject:#[#"ar"] forKey:#"AppleLanguages"];
Then, I was trying to get the preferred language and create a locale object from it using:
NSString* langPrefix = [NSLocale preferredLanguages][0];
Finally, create a new NSLocale object from the returned object. When testing the code, I would change the language from within the app, then close the app through Xcode. I am assuming that the NSUserDefaults would not synchronize, but even if I called the synchronize method, it would still screw up.
Bottom line is, testing localization should be done by deploying the app, and after the device has been disconnected from Xcode, so the app would run through all the life-cycle stages properly.
Is there any changes regarding NSDateFormatter in iOS 4.3.4? Because i upgraded my iTouch os to 4.3.4 yesterday and found that some of my code related to NSDateFormatter is stop to work.
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
NSDate *date_ = [outputFormatter dateFromString:#"08:00 AM"];
NSLog(#"Date:%#",date_);
[outputFormatter release];
OUTPUT
iOS 4.3.1 : 1970-01-01 02:30:00 +0000
iOS 4.3.4 : null.
Any ideas????
First of all, your code does NOT work in iOS 4.3.1... If you look at the date, you should notice that it is January 1970, or the common "year zero" in computer programming.
I think you should have put "HH:mm" instead of "h:mm a". The doubled h doesn't mean two digits, it is just some convention made up by ISO. And regarding the "AM-PM" parameter, I think it comes from locale.
Look at the documentation of NSDate for setLocale. Your parameter should be #"en-US" I think.
I hope it helped you.
Regards.
I'm creating an app that needs to run on 10.4 and above. Having a date string that is formatted properly is crucial. When I run my program in 10.5 debug everything is good; I get a date which is exactly how I want it:
Jul 13, 2010 16
the 16 being the hour without the minutes, which I specifically need. Then when I run it in 10.4 I get this:
07/13/10
My code to produce this date looks like this:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH"];
NSDate *newNow = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:newNow];
After reading some relevant documentation (link below), it seems like 10.4 has a unique way to to format that is different then 10.3, 10.5 and 10.6. To quote the link:
"The format string uses the format patterns from the Unicode standard (this reference is to version tr35-6 for Mac OS X v10.5; for Mac OS X v10.4 use version tr35-4)."
I've gone through the documentation and it seems like my Unicode format is okay, but I'm still getting this odd result.
Any help?
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
I think you need to set the formatter behavior to NSDateFormatterBehavior10_4 using +[NSDateFormatter setDefaultFormatterBehavior:] or -[NSDateFormatter setFormatterBehavior:].
EDIT: my reasoning is based on the comment in the NSDateFormatter overview saying:
By default, on Mac OS X v10.4
instances of NSDateFormatter have the
same behavior as they did on Mac OS X
versions 10.0 to 10.3. On Mac OS X
v10.5 and later, NSDateFormatter
defaults to the 10.4+ behavior.