diff options
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/number_format.mm | 36 | ||||
-rw-r--r-- | platform/darwin/src/string_nsstring.mm | 31 |
2 files changed, 38 insertions, 29 deletions
diff --git a/platform/darwin/src/number_format.mm b/platform/darwin/src/number_format.mm new file mode 100644 index 0000000000..86f16eb6dd --- /dev/null +++ b/platform/darwin/src/number_format.mm @@ -0,0 +1,36 @@ +#import <Foundation/Foundation.h> + +#include <mbgl/i18n/number_format.hpp> + +namespace mbgl { +namespace platform { + +std::string formatNumber(double number, const std::string& localeId, const std::string& currency, + uint8_t minFractionDigits, uint8_t maxFractionDigits) { + + static NSNumberFormatter *numberFormatter; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + numberFormatter = [[NSNumberFormatter alloc] init]; + }); + + numberFormatter.locale = !localeId.empty() ? [NSLocale localeWithLocaleIdentifier:@(localeId.c_str())] : nil; + numberFormatter.currencyCode = !currency.empty() ? @(currency.c_str()) : nil; + if (currency.empty()) { + numberFormatter.minimumFractionDigits = minFractionDigits; + numberFormatter.maximumFractionDigits = maxFractionDigits; + numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; + } else { + // Reset fraction digits to NSNumberFormatter's default values, so that the + // system will choose formatting based on number formatter locale. + numberFormatter.minimumFractionDigits = 0; + numberFormatter.maximumFractionDigits = 0; + numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; + } + NSString *formatted = [numberFormatter stringFromNumber:@(number)]; + std::string result = std::string([formatted UTF8String]); + return result; +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/darwin/src/string_nsstring.mm b/platform/darwin/src/string_nsstring.mm index 382a2acf33..d7c81236ef 100644 --- a/platform/darwin/src/string_nsstring.mm +++ b/platform/darwin/src/string_nsstring.mm @@ -27,32 +27,5 @@ std::string lowercase(const std::string &string) { return result; } -std::string formatNumber(double number, const std::string& localeId, const std::string& currency, - uint8_t minFractionDigits, uint8_t maxFractionDigits) { - - static NSNumberFormatter *numberFormatter; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - numberFormatter = [[NSNumberFormatter alloc] init]; - }); - - numberFormatter.locale = !localeId.empty() ? [NSLocale localeWithLocaleIdentifier:@(localeId.c_str())] : nil; - numberFormatter.currencyCode = !currency.empty() ? @(currency.c_str()) : nil; - if (currency.empty()) { - numberFormatter.minimumFractionDigits = minFractionDigits; - numberFormatter.maximumFractionDigits = maxFractionDigits; - numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; - } else { - // Reset fraction digits to NSNumberFormatter's default values, so that the - // system will choose formatting based on number formatter locale. - numberFormatter.minimumFractionDigits = 0; - numberFormatter.maximumFractionDigits = 0; - numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; - } - NSString *formatted = [numberFormatter stringFromNumber:@(number)]; - std::string result = std::string([formatted UTF8String]); - return result; -} - -} -} +} // namespace platform +} // namespace mbgl |