Class: Temperature

PD.Units. Temperature

Provides tools to convert between different temperature units.

Use static methods for arbitrary conversions and instance methods when converting to and/or from a specific unit multiple times. The available temperature unit types are: PD.Units.CELSIUS, PD.Units.FAHRENHEIT, PD.Units.RANKINE and PD.Units.KELVIN.


new Temperature( [units])

Creates a new temperature converter.

Parameters:
Name Type Argument Description
units number <optional>

The units type identifier, defaults to PD.Units.CELSIUS.

Example
/// Create new object to convert to/from Fahrenheit.
const fahrenheit = new PD.Units.Temperature(PD.Units.FAHRENHEIT);

/// Convert values to degrees Celsius.
const c1 = fahrenheit.toCelsius(61.7);
const c2 = fahrenheit.toCelsius(95.0);
console.log(c1, c2); // > 16.5, 35.0

/// Assign a new value from degrees Celsius.
const f1 = fahrenheit.fromCelsius(0.0);
console.log(f1); // > 32.0

/// Using static methods.
const f2 = PD.Units.Temperature.convertCelsiusToFahrenheit(16.5);
const units_abbrev = PD.Units.Temperature.getAbbrev(PD.Units.FAHRENHEIT);
const output = f2.toFixed(1) + units_abbrev; // '61.7°F'.

Members


:number

units

The numeric identifier defining the type of stored units.

The value of this property should equate to PD.Units.CELSIUS, PD.Units.FAHRENHEIT, PD.Units.RANKINE or PD.Units.KELVIN. Any other value is assumed to be Celsius.

Type
  • number

Methods


fromCelsius(degC)

Converts from degrees Celsius (°C) to the current units.

Parameters:
Name Type Description
degC number

The value in degrees Celsius (°C).

Returns:

Returns the converted value in the current units.

Type
number

fromFahrenheit(degF)

Converts from degrees Fahrenheit (°F) to the current units.

Parameters:
Name Type Description
degF number

The value in degrees Fahrenheit (°F).

Returns:

Returns the converted value in the current units.

Type
number

fromKelvin(degK)

Converts from degrees Kelvin (K) to the current units.

Parameters:
Name Type Description
degK number

The value in degrees Kelvin (K).

Returns:

Returns the converted value in the current units.

Type
number

fromRankine(degR)

Converts from degrees Rankine (°R) to the current units.

Parameters:
Name Type Description
degR number

The value in degrees Rankine (°R).

Returns:

Returns the converted value in the current units.

Type
number

getAbbrev( [no_symbols])

Retrieve the abbreviation of the current units.

Depending on the current units, this method returns one of the following string values: '°C', '°F', '°R' or 'K'.

Parameters:
Name Type Argument Description
no_symbols boolean <optional>

When true, avoids special characters by returning 'degX' instead of '°X'.

Returns:

Returns the abbreviation of the current units.

Type
string

getName()

Retrieve the full name of the current units.

Depending on the current units, this method returns one of the following string values: 'Celsius', 'Fahrenheit', 'Rankine' or 'Kelvin'.

Returns:

Returns the full name of the current units.

Type
string

getUnitsId()

Retrieves the currently units identifier as a numeric value.

Returns:

Returns the current units identifier.

Type
number

set(units)

Sets the current temperature unit and updates conversions.

Parameters:
Name Type Description
units number

The new units identifier (PD.Units.CELSIUS | FAHRENHEIT | RANKINE | KELVIN).

Returns:

Returns this Temperature object to support method chaining.

Type
object

toCelsius(value)

Converts the given value to degrees Celsius (°C).

Parameters:
Name Type Description
value number

The value to convert, must be in stored units.

Returns:

Returns the value converted to degrees Celsius (°C).

Type
number

toFahrenheit(value)

Converts the given value to degrees Fahrenheit (°F).

Parameters:
Name Type Description
value number

The value to convert, must be in stored units.

Returns:

Returns the value converted to degrees Fahrenheit (°F).

Type
number

toKelvin(value)

Converts the given value to degrees Kelvin (K).

Parameters:
Name Type Description
value number

The value to convert, must be in stored units.

Returns:

Returns the value converted to degrees Kelvin (K).

Type
number

toRankine(value)

Converts the given value to degrees Rankine (°R).

Parameters:
Name Type Description
value number

The value to convert, must be in stored units.

Returns:

Returns the value converted to degrees Rankine (°R).

Type
number

convertCelsiusToFahrenheit(degC) <static>

Converts a temperature value from Celsius to Fahrenheit.

Parameters:
Name Type Description
degC number

A value in degrees Celsius (°C).

Returns:

Returns a value in degrees Fahrenheit (°F).

Type
number

convertCelsiusToKelvin(degC) <static>

Converts a temperature value from Celsius to Kelvin (K).

Parameters:
Name Type Description
degC number

A value in degrees Celsius (°C).

Returns:

Returns a value in degrees Kelvin (K).

Type
number

convertCelsiusToRankine(degC) <static>

Converts a temperature value from Celsius to Rankine.

Parameters:
Name Type Description
degC number

A value in degrees Celsius (°C).

Returns:

Returns a value in degrees Rankine (°R).

Type
number

convertFahrenheitToCelsius(degF) <static>

Converts a temperature value from Fahrenheit to Celsius.

Parameters:
Name Type Description
degF number

A value in degrees Fahrenheit (°F).

Returns:

Returns a value in degrees Celsius (°C).

Type
number

convertFahrenheitToKelvin(degF) <static>

Converts a temperature value from Fahrenheit to Kelvin (K).

Parameters:
Name Type Description
degF number

A value in degrees Fahrenheit (°F).

Returns:

Returns a value in degrees Kelvin (K).

Type
number

convertFahrenheitToRankine(degF) <static>

Converts a temperature value from Fahrenheit to Rankine.

Parameters:
Name Type Description
degF number

A value in degrees Fahrenheit (°F).

Returns:

Returns a value in degrees Rankine (°R).

Type
number

convertKelvinToCelsius(degK) <static>

Converts a temperature value from Kelvin to Celsius.

Parameters:
Name Type Description
degK number

A value in degrees Kelvin (K).

Returns:

Returns a value in degrees Celsius (°C).

Type
number

convertKelvinToFahrenheit(degK) <static>

Converts a temperature value from Kelvin to Fahrenheit.

Parameters:
Name Type Description
degK number

A value in degrees Kelvin (K).

Returns:

Returns a value in degrees Fahrenheit (°F).

Type
number

convertKelvinToRankine(degK) <static>

Converts a temperature value from Kelvin to Rankine.

Parameters:
Name Type Description
degK number

A value in degrees Kelvin (K).

Returns:

Returns a value in degrees Rankine (°R).

Type
number

convertRankineToCelsius(degR) <static>

Converts a temperature value from Rankine to Celsius.

Parameters:
Name Type Description
degR number

A value in degrees Rankine (°R).

Returns:

Returns a value in degrees Celsius (°C).

Type
number

convertRankineToFahrenheit(degR) <static>

Converts a temperature value from Rankine to Fahrenheit.

Parameters:
Name Type Description
degR number

A value in degrees Rankine (°R).

Returns:

Returns a value in degrees Fahrenheit (°F).

Type
number

convertRankineToKelvin(degK) <static>

Converts a temperature value from Rankine to Kelvin.

Parameters:
Name Type Description
degK number

A value in degrees Rankine (°R).

Returns:

Returns a value in degrees Kelvin (K).

Type
number

getAbbrev(units [, no_symbols]) <static>

Retrieves the abbreviation of the given temperature units.

Based on the given units, this method returns one of the following string values: '°C', '°F', '°R' or 'K'.

Parameters:
Name Type Argument Description
units number

The numeric units identifier (PD.Units.CELSIUS | FAHRENHEIT | RANKINE | KELVIN).

no_symbols boolean <optional>

When true, avoids special characters by returning 'degX' instead of '°X'.

Returns:

Returns the abbreviation of the given temperature units.

Type
string

getName(units) <static>

Retrieves the name of the given temperature units.

Based on the given units, this method returns one of the following string values: 'Celsius', 'Fahrenheit', 'Rankine' or 'Kelvin'.

Parameters:
Name Type Description
units number

The numeric units identifier (PD.Units.CELSIUS | FAHRENHEIT | RANKINE | KELVIN).

Returns:

Returns the name of the given temperature units.

Type
string

getUnitsFromAbbrev(abbrev) <static>

Retrieves the temperature units identifier from the given abbreviation.

Interprets the abbreviations '°C'/'degC', '°F''degF', '°R'/'degR' or 'K' into the identifiers PD.Units.CELSIUS | FAHRENHEIT | RANKINE | KELVIN.

Parameters:
Name Type Description
abbrev string

The abbreviation of the given temperature units.

Returns:

Returns the The numeric units identifier.

Type
number