diff options
author | Juha Alanen <juha.alanen@mapbox.com> | 2019-06-03 15:34:13 +0300 |
---|---|---|
committer | Juha Alanen <19551460+jmalanen@users.noreply.github.com> | 2019-06-27 15:51:22 +0300 |
commit | 0ca8ea6f169149cd414a65f40d0f7bdd40f3cca3 (patch) | |
tree | db6a58892dd6326b8ba22da7fc822d008bdda01a /platform/darwin | |
parent | 81ea5d020efd0257083bf323644a575f5ea800c9 (diff) | |
download | qtlocation-mapboxgl-0ca8ea6f169149cd414a65f40d0f7bdd40f3cca3.tar.gz |
[core] Add number-format expression
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/string_nsstring.mm | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/platform/darwin/src/string_nsstring.mm b/platform/darwin/src/string_nsstring.mm index 08f9aeccef..096ed2b212 100644 --- a/platform/darwin/src/string_nsstring.mm +++ b/platform/darwin/src/string_nsstring.mm @@ -27,5 +27,28 @@ 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 { + numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; + } + NSString *formatted = [numberFormatter stringFromNumber:@(number)]; + std::string result = std::string([formatted UTF8String]); + return result; +} + } } |