export class UnitsConverter { private CENTIMETER_TO_METER_RATE = 0.01; private METER_TO_CENTIMETER_RATE = 100; private MINIMETER_TO_METER_RATE = 0.001; private METER_TO_MINIMETER_RATE = 1000; private KILO_TO_NORMAL_RATE = 1000; private NORMAL_TO_KILO_RATE = 0.001; private INCH_TO_METER_RATE = 0.0254; private METER_TO_INCH_RATE = 39.3700787; private FOOT_TO_METER_RATE = 0.3048; private METER_TO_FOOT_RATE = 3.2808399; private KIP_TO_NEWTON_RATE = 4448.2216; private NEWTON_TO_KIP_RATE = 0.00022480894387096; private round(roundFactor, roundedNumber) { const factor = Math.pow(10, roundFactor + 1); roundedNumber = Math.round(Math.round(roundedNumber * factor) / 10); return roundedNumber / (factor / 10); } public convertFromUnitsToSI(value: number, unit: string): number { if (unit === 'mm') { return this.convert(this.MINIMETER_TO_METER_RATE, value); } else if (unit === 'cm') { return this.convert(this.CENTIMETER_TO_METER_RATE, value); } else if (unit === 'ft') { return this.convert(this.FOOT_TO_METER_RATE, value); } else if (unit === 'inch') { return this.convert(this.INCH_TO_METER_RATE, value); } else if (unit === 'm') { return this.convert(1.0, value); } else if (unit === 'kip') { return this.convert(this.KIP_TO_NEWTON_RATE, value); } else if (unit === 'kip-ft') { return this.convert(this.KIP_TO_NEWTON_RATE * this.FOOT_TO_METER_RATE, value); } else if (unit === 'kN') { return this.convert(this.KILO_TO_NORMAL_RATE, value); } else if (unit === 'kNm') { return this.convert(this.KILO_TO_NORMAL_RATE, value); } else if (unit === 'kNcm') { return this.convert(this.KILO_TO_NORMAL_RATE * this.CENTIMETER_TO_METER_RATE, value); } } public convertFromSIToUnits(value: number, unit: string, roundFactor?: number): number { if (unit === 'mm') { return this.convert(this.METER_TO_MINIMETER_RATE, value, roundFactor); } else if (unit === 'cm') { return this.convert(this.METER_TO_CENTIMETER_RATE, value, roundFactor); } else if (unit === 'ft') { return this.convert(this.METER_TO_FOOT_RATE, value, roundFactor); } else if (unit === 'inch') { return this.convert(this.METER_TO_INCH_RATE, value, roundFactor); } else if (unit === 'm') { return this.convert(1.0, value, roundFactor); } else if (unit === 'kip') { return this.convert(this.NEWTON_TO_KIP_RATE, value, roundFactor); } else if (unit === 'kip-ft') { return this.convert(this.NEWTON_TO_KIP_RATE * this.METER_TO_FOOT_RATE, value, roundFactor); } else if (unit === 'kN') { return this.convert(this.NORMAL_TO_KILO_RATE, value, roundFactor); } else if (unit === 'kNm') { return this.convert(this.NORMAL_TO_KILO_RATE, value, roundFactor); } else if (unit === 'kNcm') { return this.convert(this.NORMAL_TO_KILO_RATE * this.METER_TO_CENTIMETER_RATE, value, roundFactor); } } public convert(rate: number, value: number, roundFactor?: number): number { let tempNumber = value * rate; if (roundFactor) { tempNumber = this.round(roundFactor, tempNumber); } return tempNumber; } }