M

UnitFormatUtils.Format

Description:
Formats a number with units into a string.
Overloads (2):
Format(Units,UnitType,Double,Boolean,Boolean)
public static string Format(
	Units units,
	UnitType unitType,
	double value,
	bool maxAccuracy,
	bool forEditing
)
  • units
    The units formatting settings, typically obtained from Document.GetUnits().
  • unitType
    The unit type of the value to format.
  • value
    The value to format, in Revit's internal units.
  • maxAccuracy
    True if the value should be rounded to an increased accuracy level appropriate for editing or understanding the precise value stored in the model. False if the accuracy specified by the FormatOptions should be used, appropriate for printed drawings.
  • forEditing
    True if the formatting should be modified as necessary so that the formatted string can be successfully parsed, for example by suppressing digit grouping. False if unmodified settings should be used, suitable for display only.
Return Value The formatted string. The formatted string.
void DisplayDensityOfMaterial(Material material)
{
    double density = 0;
    // get structural asset of material in order to get the density
    ElementId strucAssetId = material.StructuralAssetId;
    if (strucAssetId != ElementId.InvalidElementId)
    {
        PropertySetElement pse = material.Document.GetElement(strucAssetId) as PropertySetElement;
        if (pse != null)
        {
            StructuralAsset asset = pse.GetStructuralAsset();

            density = asset.Density;
            // convert the density value to a user readable string that includes the units
            Units units = material.Document.GetUnits();
            // false for maxAccuracy means accuracy specified by the FormatOptions should be used
            // false for forEditing since this will be for display only and no formatting modifications are necessary
            string strDensity = UnitFormatUtils.Format(units, UnitType.UT_UnitWeight, density, false, false);
            string msg = string.Format("Raw Value: {0}\r\nFormatted Value: {1}", density, strDensity);
            TaskDialog.Show("Material Density", msg);
        }
    }
}