Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

How Do I Convert Number Column to a More Human Readable File Size Number?

Issue

I have a column in my table with file sizes represented in bytes. I want to display them in a more user-friendly format, with decimal rounding and units like kb, gb, etc. I know how to do basic arithmetic calculations but need advice on more complex ways to achieve this.

Resolution

To display the file sizes in a more human-readable way, you can use a function defined insided a JSObject that takes the size in bytes as input and converts it to KB, MB, GB, or TB, depending on its magnitude. Here's some sample code in JavaScript:

function formatFileSize(bytes) {
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  if (bytes == 0) return '0 Bytes';
  const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

This function takes a number representing the file size in bytes and returns a string formatted with the appropriate magnitude and rounding to two decimal places. To use this function in your table, you can update the computed value of your column so it calls the function 

{{JSObject1.formatFileSize(currentRow["file_size"])}}

With this setup, the file sizes in your table should now be displayed in a more human-readable way.