npm.nicfv.com
    Preparing search index...

    Unit Conversions

    BACK | 1 | NEXT | Up next: Learn how to use Dimensional for dimensional analysis!

    Let's start with a simple example, to calculate BMI where all the inputs are US customary units. BMI is your mass in kilograms divided by your height in meters, squared.

    $$\text{BMI} = \frac{m_{kg}}{h_{m}^{2}}$$

    Let's assume you are 5'9" and weigh 140 lb 8 oz.

    We'll first need to convert height from from feet and inches to SI units, or meters. Use Dimensional to define quantities of feet and inches, add them together, and convert them to meters. Then, we'll do the same for weight; from pounds (mass) and ounces to kilograms. We'll finally use these quantities to calculate BMI. Be sure to copy and paste the output into an online LaTeX editor to view the a cleaner version of the result!

    Follow these steps to create a new project workspace and install the dimensional dependency to run this example.

    # Create and open project folder
    mkdir Unit_Conversions_demo
    cd Unit_Conversions_demo
    # Initialize project and install dependencies
    npm init -y
    npm i dimensional@1.3.1
    # Create and open source file
    touch "Unit Conversions.mjs"
    open "Unit Conversions.mjs"

    Copy and paste this source code into Unit Conversions.mjs.

    import { config, Quantity, units } from 'dimensional';

    // Display a maximum of 2 decimal points
    config.decimalsShown = 2;

    // ==== INPUTS ====//

    // Define your height in feet and inches
    const height_ft = new Quantity(5, units.foot),
    height_in = new Quantity(9, units.inch);
    // Define your weight in pounds and ounces (mass)
    const mass_lb = new Quantity(140, units.poundMass),
    mass_oz = new Quantity(8, units.ounce);

    // ==== CONVERSIONS ====//

    // Calculate your total height in feet
    const height_us = height_ft.plus(height_in); // Automatically converts to ft because the first argument's units are ft
    console.log('H_{us} = ' + height_us.toString());
    // Convert to SI units
    const height_si = height_us.as(units.meter);
    console.log('H_{si} = ' + height_si.toString());

    // Calculate your total weight in pounds
    const mass_us = mass_lb.plus(mass_oz);
    console.log('M_{us} = ' + mass_us.toString());
    // Convert to SI units
    const mass_si = mass_us.as(units.kilogram);
    console.log('M_{si} = ' + mass_si.toString());

    // ==== CALCULATE BMI ====//

    // Use the formula with SI units (m/h^2)
    const BMI = mass_si.over(height_si.pow(2));
    console.log('BMI = ' + BMI.toString());
    // Define healthy BMI range
    const healthy_BMI_min = 18.5;
    const healthy_BMI_max = 24.9;
    // Use .quantity for inequality comparisons
    if (BMI.quantity < healthy_BMI_min) {
    console.log('Underweight.');
    } else if (BMI.quantity < healthy_BMI_max) {
    console.log('Healthy weight.');
    } else {
    console.log('Overweight.');
    }

    In Unit_Conversions_demo/, execute Unit Conversions.mjs with NodeJS to generate an output.

    node "Unit Conversions.mjs"
    

    You should expect to see an output similar to the one below.

    H_{us} = 5.75 \left[ \text{ft} \right]
    H_{si} = 1.75 \left[ \text{m} \right]
    M_{us} = 140.5 \left[ {\text{lb}_{m}} \right]
    M_{si} = 63.73 \left[ {\text{k}\text{g}} \right]
    BMI = 20.75 \left[ \frac{{\text{k}\text{g}}}{\text{m}^{2}} \right]
    Healthy weight.