Base: [skip ci] implement method Quantity.toStr() to control string representation

This commit is contained in:
wmayer
2020-02-29 22:51:55 +01:00
parent d99f424345
commit 9415e7b6da
2 changed files with 28 additions and 0 deletions

View File

@@ -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