How to use the unit function from mathjs

Find comprehensive JavaScript mathjs.unit code examples handpicked from public code repositorys.

-3
fork icon19
star icon29
watch icon6

+ 19 other calls in file

344
345
346
347
348
349
350
351
352
353
354
    return { valueSI: value, unitSI: unit };
  }
};


exports.convertToRequestedUnit = (value, currentUnit, requestedUnit) => {
  const converted = math.unit(value, currentUnit).to(requestedUnit);
  const formatted = math.format(converted, { precision: 3 }).toString();
  const convertedValue = formatted.substring(0, formatted.indexOf(" "));
  const convertedUnit = formatted.substring(formatted.indexOf(" ") + 1);
  return {
fork icon15
star icon13
watch icon9

+ 5 other calls in file

830
831
832
833
834
835
836
837
838
839
    return true;

var isValid = true;

try {
    mathjs.unit(unitName);
}
catch {
    isValid = false;
}
fork icon0
star icon3
watch icon0

+ 7 other calls in file

5
6
7
8
9
10
11
12
13
14
15


export class Electrostatics {
    // Pass the charge of the given object/s as input as well as their distance which is optional
    // Example input for charges
    constructor(charges, distance, radiusEquation) {
        this.coulombConstant = unit(9.0e9, 'N m^2 C^-2')
        // console.log(coulombConstant.toString())
        this.info = `\t**Given:**\n\t   - Charges:`
        let userInputCharges = charges.split(/(?<=[cC])\s*/g)
        this.parsedCharges = userInputCharges.map((input, index) => {
fork icon0
star icon1
watch icon0

35
36
37
38
39
40
41
42
43
44
this.initialVelocity.actual = unit(this.initialVelocity.actual);
this.initialVelocity.rounded = clone(this.initialVelocity.actual);
this.initialVelocity.rounded.value = SigFig(this.initialVelocity.rounded.value, this.roundToSigFig)
// Analyze the given, check what are the missing info that is needed to be solved...
if (this.height.actual == undefined && this.time.actual == undefined) {
    this.finalVelocity.actual = unit(this.finalVelocity.actual);
    this.finalVelocity.rounded = clone(this.finalVelocity.actual);
    this.finalVelocity.rounded.value = SigFig(this.finalVelocity.rounded.value, this.roundToSigFig) 
    // First find the height using the formula d = vf^2 - vi^2 / 2g 
    this.height.actual = divide(subtract(square(this.finalVelocity.actual), square(this.initialVelocity.actual)), multiply(2, gravity));
fork icon0
star icon1
watch icon0

+ 28 other calls in file

143
144
145
146
147
148
149
150
151
// TODO: Do equivalent solutes and discord output
//  equivalent weight is already given
equivalentOfSolute: (massSolute, equivalentWeight) => {
    if (massSolute != undefined && equivalentWeight != undefined) {
        let temp = divide(massSolute, equivalentWeight)
        return unit(temp, "eq")
    }         
    return undefined;
},
fork icon0
star icon1
watch icon0

+ 32 other calls in file

5
6
7
8
9
10
11
12
13
    } else {
        if (!units) {
            units = 'ft';
        }

        this.distance = math.unit(value,units);
    }

}
fork icon0
star icon1
watch icon6

+ 9 other calls in file

78
79
80
81
82
83
84
85
86
var c = math.complex(3, -4);    // 3 - 4i
math.sqrt(c);                   // 2 - i

math.sqrt(-4);                  // 2i

var f = math.unit(60, 'deg');   // 60 deg
var g = math.cos(f);            // 0.5
```

fork icon0
star icon0
watch icon2

+ 47 other calls in file