使用 AngularJS 将 AngularFire JSON return 整数值转换为十进制数(100 是 1.00)
Convert AngularFire JSON return integer values to decimal numbers (100 is 1.00) using AngularJS
我正在尝试转换从 JSON 来源(通过 AngularFire 的 Firebase)返回的数据,并将其显示为美元金额。未来数据以美分存储 API,Firebase 假装是为了在用户界面上进行早期工作。
我知道货币过滤器会转换值并截断小数、插入货币符号等...,但我还需要将收到的数据从美分 (1234) 转换为等值的美元 (12.34)。
样本:
angular.module('MyApp').factory("StoreItems", ["$firebaseObject", "$firebaseArray", "GetFireBaseObject",
function($firebaseObject, $firebaseArray, GetFireBaseObject) {
var StoreItemsRef = GetFireBaseObject.DataURL('StoreItems/');
return {
AllStores: function() {
return $firebaseObject(StoreItemsRef);
},
OneStore: function(StoreKey) {
var OneStoreRef = StoreItemsRef.child(StoreKey);
return $firebaseObject(OneStoreRef);
},
OneStoreItems: function(StoreKey) {
var OneStoreRef = StoreItemsRef.child(StoreKey);
return $firebaseArray(OneStoreRef);
}
};
}
]);
angular.module('MyApp').controller("StoreItemsCtrl", ["$scope", "StoresData", "StoreItems",
function($scope, StoresData, StoreItems) {
var StoreData = {};
$scope.StoreList = StoresData.AllStores();
$scope.SelectedStoreKey = null;
$scope.LoadStoreData = function(StoreKey) {
$scope.SelectedStore = StoresData.OneStore(StoreKey);
$scope.StoreItemData = StoreItems.OneStoreItems(StoreKey);
StoreData = StoreItems.OneStoreItems(StoreKey);
$scope.StoreItemsGrid.data = StoreData;
};
columnDefs = [
{ field: "Key", displayName: "Product Code", visible: true, groupable: false, resizable: true, sortable: true },
{ field: "UnitPrice", displayName: "Unit Price", visible: true, groupable: false, resizable: true, sortable: true, cellFilter: 'currency' },
{ field: "nForPrice", displayName: "nFor Price", visible: true, groupable: false, resizable: true, sortable: true },
{ field: "nForQuantity", displayName: "nFor Qty", visible: true, groupable: true, resizable: true, sortable: true }
];
$scope.StoreItemsGrid = {
enableSorting: true
};
$scope.StoreItemsGrid.columnDefs = columnDefs;
$scope.StoreItemsGrid.data = StoreData;
}
]);
如果您仍想使用 currenyFilter,您可以创建自己的过滤器以简单地除以 100 并将结果通过货币过滤器。
angular.module('MyApp').filter('coinCurrency', function($filter){
var currencyFilter = $filter('currency');
return function(input){
return currencyFilter(input / 100);
};
});
我正在尝试转换从 JSON 来源(通过 AngularFire 的 Firebase)返回的数据,并将其显示为美元金额。未来数据以美分存储 API,Firebase 假装是为了在用户界面上进行早期工作。
我知道货币过滤器会转换值并截断小数、插入货币符号等...,但我还需要将收到的数据从美分 (1234) 转换为等值的美元 (12.34)。
样本:
angular.module('MyApp').factory("StoreItems", ["$firebaseObject", "$firebaseArray", "GetFireBaseObject",
function($firebaseObject, $firebaseArray, GetFireBaseObject) {
var StoreItemsRef = GetFireBaseObject.DataURL('StoreItems/');
return {
AllStores: function() {
return $firebaseObject(StoreItemsRef);
},
OneStore: function(StoreKey) {
var OneStoreRef = StoreItemsRef.child(StoreKey);
return $firebaseObject(OneStoreRef);
},
OneStoreItems: function(StoreKey) {
var OneStoreRef = StoreItemsRef.child(StoreKey);
return $firebaseArray(OneStoreRef);
}
};
}
]);
angular.module('MyApp').controller("StoreItemsCtrl", ["$scope", "StoresData", "StoreItems",
function($scope, StoresData, StoreItems) {
var StoreData = {};
$scope.StoreList = StoresData.AllStores();
$scope.SelectedStoreKey = null;
$scope.LoadStoreData = function(StoreKey) {
$scope.SelectedStore = StoresData.OneStore(StoreKey);
$scope.StoreItemData = StoreItems.OneStoreItems(StoreKey);
StoreData = StoreItems.OneStoreItems(StoreKey);
$scope.StoreItemsGrid.data = StoreData;
};
columnDefs = [
{ field: "Key", displayName: "Product Code", visible: true, groupable: false, resizable: true, sortable: true },
{ field: "UnitPrice", displayName: "Unit Price", visible: true, groupable: false, resizable: true, sortable: true, cellFilter: 'currency' },
{ field: "nForPrice", displayName: "nFor Price", visible: true, groupable: false, resizable: true, sortable: true },
{ field: "nForQuantity", displayName: "nFor Qty", visible: true, groupable: true, resizable: true, sortable: true }
];
$scope.StoreItemsGrid = {
enableSorting: true
};
$scope.StoreItemsGrid.columnDefs = columnDefs;
$scope.StoreItemsGrid.data = StoreData;
}
]);
如果您仍想使用 currenyFilter,您可以创建自己的过滤器以简单地除以 100 并将结果通过货币过滤器。
angular.module('MyApp').filter('coinCurrency', function($filter){
var currencyFilter = $filter('currency');
return function(input){
return currencyFilter(input / 100);
};
});