javascriptmeteornumbersformatsystem-information

easiest way to format long numbers in javascript to the right unit?


I am building a web app that can show system information on a website. It shows the hardware information (temps, ram, cpu, fan speeds and so on) of the pc the "server" is running on. It opens a web server showing this information which is accessible over the network. I am using meteor, blazejs and systeminformation ( https://github.com/sebhildebrandt/systeminformation ) to do this. System information shows the information it gets from the server as long numbers. This is my html:

<template name="main">
{{#if subscriptionsReady}}

  <p>cpu manufacturer: {{get (static) 'data.cpu.manufacturer'}}</p>
  <p>cpu brand: {{get (static) 'data.cpu.brand'}}</p>
  <p>cpu model: {{get (static) 'data.cpu.model'}}</p>
  <p>cpu speed: {{get (static) 'data.cpu.speed'}}</p>
  <p>cpu cores: {{get (static) 'data.cpu.cores'}}</p>

  <p>total memory: {{get (dynamic) 'data.mem.total'}}</p>
  <p>free memory: {{get (dynamic) 'data.mem.free'}}</p>
  <p>used memory: {{get (dynamic) 'data.mem.used'}}</p>
  <p>active memory: {{get (dynamic) 'data.mem.active'}}</p>
{{/if}}

which shows this on the website

website output

I would like to format the long numbers to look like "total memory X.yz GB" and similar for different data. Is there any easy to use library out there I could use for this?

Also here is my server javascript file

Meteor.startup(() => {

si.getStaticData(Meteor.bindEnvironment(function(data){
    Data.update({type: 'static'}, {data: data, type: 'static'}, {upsert: true})
}))

setInterval(Meteor.bindEnvironment(function() {

    si.getDynamicData(Meteor.bindEnvironment(function(data){
        Data.update({type: 'dynamic'}, {data: data, type: 'dynamic'}, {upsert: true})
    }))

}), 1000)

Meteor.publish('data', function(){
    return Data.find({})
})
});

and my client javascript

let subscriptions = [
  Meteor.subscribe('data')
]

Template.main.helpers({

  get: function(obj, what) {
      console.log(obj)
      return _.get(obj, what)
},

dynamic: function() {
    return Data.findOne({type: 'dynamic'})
},

static: function() {
    return Data.findOne({type: 'static'})
},

subscriptionsReady: function() {
    for (let sub of subscriptions){
        if (!sub.ready()) return false
    }
    return true
}
})

Solution

  • There is the very popular pretty-bytes library (MIT license):

    Convert bytes to a human readable string: 1337 → 1.34 kB

    Useful for displaying file sizes for humans.

    Usage:

    const prettyBytes = require('pretty-bytes');
    
    prettyBytes(1337);
    //=> '1.34 kB' 
    
    prettyBytes(100);
    //=> '100 B'
    

    Note that it converts in base 10 (i.e. 1 GB = 1,000,000,000 B like what storage manufacturers use in general).

    As I guess you are referring to RAM memory, which I think is reported by manufacturers using base 2 conversion (i.e. 1 GiB = 1,024 * 1,024 * 1,024 B), you might be interested instead into this other popular library: filesize (BSD 3-Clause license)