使用AngularJS将AngularFire JSON返回整数值转换为十进制数(100为1.00)

时间:2022-01-27 18:55:51

I am trying to convert data that is returned from a JSON source (Firebase via AngularFire) and have it display as a dollar amount. The data is stored in cents in the future API, which Firebase is pretending to be for early work on the User Interface.

我正在尝试转换从JSON源(Firebase通过AngularFire)返回的数据,并将其显示为美元金额。数据存储在未来API中的分数中,Firebase假装用于用户界面的早期工作。

I know that the currency filter will convert the value and truncate decimals, insert currency symbols etc..., but I need to convert the data received as well from cents (1234) to the dollar equivalent (12.34).

我知道货币过滤器将转换值并截断小数,插入货币符号等...但我需要将收到的数据从美分(1234)转换为美元等值(12.34)。

Sample:

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;
    }
]);

1 个解决方案

#1


1  

If you still want to use the currenyFilter you can create your own filter to simply divide by 100 and pass the result through the currency filter.

如果您仍想使用currenyFilter,您可以创建自己的过滤器,只需除以100并通过货币过滤器传递结果。

angular.module('MyApp').filter('coinCurrency', function($filter){
    var currencyFilter = $filter('currency');
    return function(input){
        return currencyFilter(input / 100);
    };
});

#1


1  

If you still want to use the currenyFilter you can create your own filter to simply divide by 100 and pass the result through the currency filter.

如果您仍想使用currenyFilter,您可以创建自己的过滤器,只需除以100并通过货币过滤器传递结果。

angular.module('MyApp').filter('coinCurrency', function($filter){
    var currencyFilter = $filter('currency');
    return function(input){
        return currencyFilter(input / 100);
    };
});