Android: How to identify if device model uses GB or GiB

Viewed 87

We have a requirement on our app to display storage stats of device inside a screen of our cordova hybrid app. We are able to obtain the stats in bytes from OS. It was observed that some device models use 1000^3 as base to convert bytes to GB while others use 1024^3. Inorder to identify the correct base for conversion, the following code was implemented.

 _setConversionFactor(totalBytes) {
        var storageMaxPossibleValues = [1,2,4,8,16,32,64,128,256,512,1024]

        var convertedValue = totalBytes * 100 / (this.conversionBaseValue) / 100;

        if(!storageMaxPossibleValues.includes(Math.round(convertedValue))){
            this.conversionBaseValue = 1 << 30;
        }
        console.debug("conversion base-> "+ this.conversionBaseValue);
    }
Initial value of this.conversionBaseValue is 10e9. Is this implementation right? Is there any better way to implement this?

0 Answers
Related