我想要货币代码中的货币符号。 例如)EUR - > €,USD - > $,SEK - > kr,DKK - > KR
我使用下面的代码来获取货币符号。
from flask import Flask, abort, request
import signal
import threading
import time
import os
import requests
app = Flask(__name__)
shuttingDown = False
def exit_call():
time.sleep(20)
requests.post("http://localhost:5420/_shutdown")
# os._exit(0)
def exit_gracefully(self, signum):
app.logger.error('Received shutdown signal. Exiting gracefully')
global shuttingDown
shuttingDown = True
# TODO: wait for some time here to ensure we are not receiving any more
# traffic
_et = threading.Thread(target=exit_call)
_et.daemon = True
_et.start()
signal.signal(signal.SIGTERM, exit_gracefully)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/_status/liveness")
def liveness():
return "I am alive"
@app.route("/_shutdown", methods=["POST"])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
return "Not a werkzeug server"
func()
return "shutdown"
@app.route("/_status/readiness")
def readiness():
if not shuttingDown:
return "I am ready"
else:
abort(500, 'not ready anymore')
app.run(port=5420)
但它为SEK返回SEK,为DKK返回DKK,它应该返回kr。 美元,英镑,欧元工作正常。
可能是什么问题?
答案 0 :(得分:3)
它不使用NSLocale
类(Swift 3/4):
func getSymbolForCurrencyCode(code: String) -> String? {
let result = Locale.availableIdentifiers.map { Locale(identifier: $0) }.first { $0.currencyCode == code }
return result?.currencySymbol
}
getSymbolForCurrencyCode(code: "GBP")
答案 1 :(得分:0)
我为字符串创建了两个扩展,添加了缓存以优化搜索。
extension String {
private static let currencyCode = NSCache<NSString, NSString>()
public var symbolForCurrencyCode: String? {
if let cached = Self.currencyCode.object(forKey: self as NSString) {
return cached as String
}
let identifiers = Locale.availableIdentifiers
guard let identifier = identifiers.first(where: { Locale(identifier: $0).currencyCode == self }) else {
return nil
}
guard let symbol = Locale(identifier: identifier).currencySymbol else {
return nil
}
Self.currencyCode.setObject(symbol as NSString, forKey: self as NSString)
return symbol
}
}
extension Optional where Wrapped == String {
public var symbolForCurrencyCode: String? {
guard let code = self else {
return nil
}
return code.symbolForCurrencyCode
}
}
"GBP".symbolForCurrencyCode // "£"
"EUR".symbolForCurrencyCode // "€"
"SEK".symbolForCurrencyCode // "kr"