Skip to content

Commit

Permalink
Base: [skip ci] implement method Quantity.toStr() to control string r…
Browse files Browse the repository at this point in the history
…epresentation
  • Loading branch information
wwmayer committed Feb 29, 2020
1 parent 5947a5b commit c165edd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Base/QuantityPy.xml
Expand Up @@ -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>
Expand Down
19 changes: 19 additions & 0 deletions src/Base/QuantityPyImp.cpp
Expand Up @@ -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
Expand Down

0 comments on commit c165edd

Please sign in to comment.