pydicom.valuerep.format_number_as_ds

pydicom.valuerep.format_number_as_ds(val: Union[float, decimal.Decimal]) str[source]

Truncate a float’s representation to give a valid Decimal String (DS).

DICOM’s decimal string (DS) representation is limited to strings with 16 characters and a limited set of characters. This function represents a float that satisfies these constraints while retaining as much precision as possible. Some floats are represented using scientific notation to make more efficient use of the limited number of characters.

Note that this will incur a loss of precision if the number cannot be represented with 16 characters. Furthermore, non-finite floats (infs and nans) cannot be represented as decimal strings and will cause an error to be raised.

Parameters

val (Union[float, Decimal]) – The floating point value whose representation is required.

Returns

String representation of the float satisfying the constraints of the decimal string representation.

Return type

str

Raises

ValueError – If val does not represent a finite value