我使用money.js和accounting.js库。我使用npm install来获取它们在package.json中,并在我的JS文件中引用它们,如:
var fx = require("money");
var accounting = require("accounting");
然而,库中包含的函数无法识别,而角度无法加载 - 所以我认为我必须这样做不正确。
这就是它的使用方式:
var fx = require("money");
var accounting = require("accounting");
const USD = "USD";
const GBP = "GBP";
const EUR = "EUR";
fx.base = USD;
var response = $http.get("http://api.fixer.io/latest");
response.then(function(response){
fx.rates = response.data.rates;
}, function(error){
//handle error
});
...
$scope.getTotalGBP = () => {
var price = $scope.getTotalPrice();
var priceGBP = fx.convert(price).from(USD).to(GBP);
var formatted = accounting.formatMoney(priceGBP, [symbol = "£"], [precision = 2], [thousand = ","], [decimal = "."], [format = "%s%v"]);
return formatted;
};