# Measure Code Examples

This page provides example Ruby code for doing various common tasks. It is intended as a supplement to the full OpenStudio SDK documentation. The examples are not complete Measures for the sake of clarity.

## Unit Conversion

OpenStudio has a built-in unit conversion method:

```
OpenStudio.convert()
```

This method will convert a value from the first unit to the second unit, as specified by two strings. The conversion can be from IP to SI, SI to IP, IP to another IP unit, or SI to another SI unit. Any to-from units that are valid conversions will result in an initialized Optional::Double (see [this] for an explanation of optional doubles).

```
# Convert the value 212 from Fahrenheit to Celsius
val_in_celsius = OpenStudio.convert(val_in_fahrenheit,"F","C")
# Check that the conversion worked, then get the value
if val_in_celsius.is_initialized
val_in_celsius = val_in_celsius.get
end
```

```
# Or, if you know the unit conversion is valid, here's the short version
val_in_celsius = OpenStudio.convert(val_in_fahrenheit,"F","C").get
```

You can use the `*`

(multiply), `/`

(divide), and `^`

(exponent) operators in the unit strings to combine basic units like `"ft"`

and `"min"`

into compound units like `"ft^3/min"`

.

### Available units

Unit Type | IP | SI |
---|---|---|

Temperature | "F" | "C" |

DeltaTemperature | "R" | "K" |

Length | "in", "ft", "mi" | "m" |

People | "person" | "person" |

Time | "s", "min", "h", "hr", "day" | "s", "min", "h", "hr", "day" |

Mass | "lb_m", "ton" | "kg" |

Area | "in^2", "ft^2" | "m^2" |

Volume | "in^3", "ft^3" | "m^3", "L" |

Force | "lb_f" | "N" |

Power | "ftlb_f/s" |
"W" |

Pressure | "inH_{2}O", "ftH_{2}O", "inHg" | "Pa" |

FlowRate | "cfm", "ft^3/min", "gal/min", "gpd", "gal/day" | "m^3/hr", "m^3/s", "L/hr", "L/s", "L/min", "L/day" |

U-Factor | "Btu/ft^2hR" |
"W/m^2K" |

R-Value | "ft^2hR/Btu" |
"m^2K/W" |

Conductivity | "Btu/ft^2hR" |
"W/m^2K" |

Illuminance | "fc" | "lux" |

LuminousFlux | "lm" | |

Angles | "deg" | "rad" |

Energy | "Btu", "kBtu", "ft*lb_f" | "J", "GJ" |

RotationalSpeed | "rpm" | "rpm" |

Cycles | "cycle" | "cycle" |

Frequency | "Hz" | "Hz" |

Velocity | "mph" | |

ElectricPotential | "V" | "V" |

ElectricResistance | "ohm" | "ohm" |