Convert Bytes to GB using javascriot and getting values from the sum of a column in ng-repeat [duplicate]

This question already has an answer here:

I am getting the sum of the values from a column of my ng-repeat. I am getting it already but the value is in bytes. I need to convert it directly to GB in order for me to use in on my other opertion. My code is like this. At first I am getting the sum of my column in ng-repeat like this:

$scope.getTotal = function(type) {

        var totalStorage = $scope.totalStorage;

        var usedtotal = 0;
        var total =0;
        for(var i = 0; i < $scope.adList.length; i++){
           var product = $scope.adList[i];
           total += JSON.parse(product.size );
       }

and after the for loop, i am converting the value, I am using this code but it only converts the value to MB.

var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
           if (total == 0) return '0 Byte';
           var i = parseInt(Math.floor(Math.log(total) / Math.log(1024)));
           return Math.round(total / Math.pow(1024, i), 2) + ' ' + sizes[i];


    return total;
    };

I am also quite not sure about the conversion because the sum I have is 30150489 bytes and when using this code, it converts to 29MB not 30.15MB. and I need to convert this directly to GB. How can I do that?

Source: AngularJS