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 5947a5bad5
commit c165edd52c
2 changed files with 28 additions and 0 deletions

View File

@@ -27,6 +27,15 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity
</UserDocu>
<DeveloperDocu>Quantity</DeveloperDocu>
</Documentation>
<Methode Name="toStr" Const="true">
<Documentation>
<UserDocu>
toStr([decimals])
returns a string representation rounded to number of decimals. If no decimals are specified then
the internal precision is used
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getUserPreferred" Const="true">
<Documentation>
<UserDocu>

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