Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Angularjs Convert Size (Bytes) to KB, MB, GB, TB in jQuery or JavaScript

May 5, 2015
Introduction

Here I will explain how to convert size bytes to kb, mb, gb or tb in
AngularJS or JavaScript or jQuery. By "Math" formulas we can convert size bytes to kb, mb, gb or tb in AngularJS or jQuery or JavaScript.


To get convet size (bytes) to kb, mb, gb or tb using AngularJS we need to write the code like as shown below


$scope.fnconvertsize = function () {
var bytessize = $scope.sizeinbytes;
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytessize == 0)
$scope.convertedsize= '0 Byte';
var i = parseInt(Math.floor(Math.log(bytessize) / Math.log(1024)));
$scope.convertedsize = Math.round(bytessize / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

If you want to check it in complete example you need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Convert size (Bytes) to KB, MB, GB, TB in Angularjs</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module('sampleapp', [])
myApp.controller("expressionController", function ($scope) {
$scope.fnconvertsize = function () {
var bytessize = $scope.sizeinbytes;
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytessize == 0)
$scope.convertedsize= '0 Byte';
var i = parseInt(Math.floor(Math.log(bytessize) / Math.log(1024)));
$scope.convertedsize = Math.round(bytessize / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
});
</script>
</head>
<body>
<form id="form1">
<div data-ng-app="sampleapp" data-ng-controller="expressionController">
<input type="number" ng-model="sizeinbytes"> <br/><br/>
<input type="button" ng-click="fnconvertsize()" value="Convert Size" /><br/><br/>
Converted Size : <span id="cdefault">{{convertedsize}}</span><br/>
</div>
</form>
</body>
</html>

Live Demo

For live demo try to enter values in textbox and click button to get conversion size






Converted Size : {{convertedsize}}


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.