diff --git a/src/Base/QuantityPy.xml b/src/Base/QuantityPy.xml index f4c487157b38..7eb0edfabf69 100644 --- a/src/Base/QuantityPy.xml +++ b/src/Base/QuantityPy.xml @@ -27,6 +27,15 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity Quantity + + + + toStr([decimals]) + returns a string representation rounded to number of decimals. If no decimals are specified then + the internal precision is used + + + diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index a1e3f1f7e2e6..cf3079f6ed92 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -55,6 +55,25 @@ std::string QuantityPy::representation(void) const return ret.str(); } +PyObject* QuantityPy::toStr(PyObject* args) +{ + int prec = getQuantityPtr()->getFormat().precision; + if (!PyArg_ParseTuple(args,"|i", &prec)) + return nullptr; + + double val= getQuantityPtr()->getValue(); + Unit unit = getQuantityPtr()->getUnit(); + + std::stringstream ret; + ret.precision(prec); + ret.setf(std::ios::fixed, std::ios::floatfield); + ret << val; + if (!unit.isEmpty()) + ret << " " << unit.getString().toUtf8().constData(); + + return Py_BuildValue("s", ret.str().c_str()); +} + PyObject *QuantityPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper { // create a new instance of QuantityPy and the Twin object