Ninjakreborn Posted July 23, 2011 Share Posted July 23, 2011 <script type="text/javascript"> function data_calculator() { var data = $('#data').val(); var data_one_conversion_type = $('#data_one_conversion_type').val(); var data_two_conversion_type = $('#data_two_conversion_type').val(); var data_storage_type = $('#data_storage_type').val(); var value_modifier = 0; if (data_storage_type == 'Processor or Virtual Storage') { value_modifier = 1024; }else if (data_storage_type == 'Disk Storage') { value_modifier = 1000; } var calculation = ''; switch(data_one_conversion_type) { case 'bits': switch(data_two_conversion_type) { case 'bits': calculation = data; break; case 'bytes': calculation = data / 8; calculation = bytesToSize(calculation, 'byte') break; case 'kb': calculation = data / 8; calculation = bytesToSize(calculation, 'kb'); break; case 'mb': calculation = data / 8; calculation = bytesToSize(calculation, 'mb'); break; case 'gb': calculation = data / 8; calculation = bytesToSize(calculation, 'gb'); break; case 'tb': calculation = data / 8; calculation = bytesToSize(calculation, 'tb'); break; case 'pb': calculation = data / 8; calculation = bytesToSize(calculation, 'pb'); break; case 'eb': calculation = data / 8; calculation = bytesToSize(calculation, 'eb'); break; case 'zb': calculation = data / 8; calculation = bytesToSize(calculation, 'zb'); break; case 'yb': calculation = data / 8; calculation = bytesToSize(calculation, 'yb'); break; case 'bb': calculation = data / 8; calculation = bytesToSize(calculation, 'bb'); break; } break; } $('#data_value').val(data + " " + data_one_conversion_type + ' = ' + calculation + " " + data_two_conversion_type); } function bytesToSize(bytes, data_format) { // Base values var kilobyte = 1024; var megabyte = kilobyte * 1024; var gigabyte = megabyte * 1024; var terabyte = gigabyte * 1024; var petabyte = terabyte * 1024; var exabyte = petabyte * 1024; var zettabyte = exabyte * 1024; var yottabyte = zettabyte * 1024; var brontobyte = yottabyte * 1024; if (data_format == 'byte') { return bytes; }else if (data_format == 'kb') { return bytes / kilobyte; }else if (data_format == 'mb') { return bytes / megabyte; }else if (data_format == 'gb') { return bytes / gigabyte; }else if (data_format == 'tb') { return bytes / terabyte; }else if (data_format == 'pb') { return bytes / petabyte; }else if (data_format == 'eb') { return bytes / exabyte; }else if (data_format == 'zb') { return bytes / zettabyte; }else if (data_format == 'yb') { return bytes / yottabyte; }else if (data_format == 'bb') { return bytes / brontobyte; } } </script> <p>This form allows you to convert one data amount, to another.</p> <form name="data_converter" id="data_converter" class="standard" method="post" action="Javascript: void();" onsubmit="data_calculator();"> <label for="data_storage_type">Data Storage Type:</label> <select name="data_storage_type" id="data_storage_type"> <option value="Processor or Virtual Storage">Processor or Virtual Storage (1024)</option> <option value="Disk Storage">Disk Storage (1000)</option> </select><br /> <label for="data">Data Amount:</label> <input name="data" id="data" type="text" /><br /> <label for="data_one_conversion_type">Conversion Type:</label> <select name="data_one_conversion_type" id="data_one_conversion_type"> <option value="bits">Bits</option> <option value="bytes">Bytes</option> <option value="kb">Kilobytes</option> <option value="mb">Megabytes</option> <option value="gb">Gigabytes</option> <option value="tb">Terabytes</option> <option value="pb">Petabytes</option> <option value="eb">Exabytes</option> <option value="zb">Zettabytes</option> <option value="yb">Yottabytes</option> <option value="bb">Brontobytes</option> </select><br /> <label for="data_two_conversion_type">Conversion Type:</label> <select name="data_two_conversion_type" id="data_two_conversion_type"> <option value="bits">Bits</option> <option value="bytes">Bytes</option> <option value="kb">Kilobytes</option> <option value="mb">Megabytes</option> <option value="gb">Gigabytes</option> <option value="tb">Terabytes</option> <option value="pb">Petabytes</option> <option value="eb">Exabytes</option> <option value="zb">Zettabytes</option> <option value="yb">Yottabytes</option> <option value="bb">Brontobytes</option> </select><br /> <label for="data_value">Data Value</label> <input name="data_value" id="data_value" type="text" /> <input name="submit" id="submit" type="submit" value="Convert" /> </form> Very simply. I want to build a converter. I want this converter to handle all data formats. I can't seem to get my calculations the same as what I am finding online (or on Google). I have tried to set this up in 10-15 different ways, but none of them are working. I have tried using Math.pow, and a variety of other attempts. I have tried handling the calculation in a variety of ways. I have not built out the other conversions because I am having trouble even on the bits. Right now I am trying to get even semi correct results before I working on taking into account the two data format types. For example.... You can see the live results of the code listed above here: http://www.onlinewasteland.com/index.php/tools/extremeconverter Any advice is appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.