Base: make UnitsApi::toNumber method of Quantity
This static method takes Quantity as a parameter, so make it method of Quantity directly.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <numbers>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
@@ -243,7 +244,26 @@ Quantity Quantity::operator-() const
|
||||
|
||||
std::string Quantity::toString(const QuantityFormat& format) const
|
||||
{
|
||||
return fmt::format("'{} {}'", UnitsApi::toNumber(myValue, format), myUnit.getString());
|
||||
return fmt::format("'{} {}'", toNumber(format), myUnit.getString());
|
||||
}
|
||||
|
||||
std::string Quantity::toNumber(const QuantityFormat& format) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
switch (format.format) {
|
||||
case QuantityFormat::Fixed:
|
||||
ss << std::fixed;
|
||||
break;
|
||||
case QuantityFormat::Scientific:
|
||||
ss << std::scientific;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ss << std::setprecision(format.precision) << myValue;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string Quantity::getUserString() const
|
||||
|
||||
@@ -160,6 +160,9 @@ public:
|
||||
std::string
|
||||
toString(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const;
|
||||
|
||||
std::string
|
||||
toNumber(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const;
|
||||
|
||||
std::string getUserString() const;
|
||||
/// transfer to user preferred unit/potence
|
||||
std::string getUserString(double& factor, std::string& unitString) const;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#include <CXX/WrapPython.h>
|
||||
@@ -93,30 +92,6 @@ void UnitsApi::setSchema(const size_t num)
|
||||
schemas->select(num);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(const Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
return toNumber(quantity.getValue(), format);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(const double value, const QuantityFormat& format)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
switch (format.format) {
|
||||
case QuantityFormat::Fixed:
|
||||
ss << std::fixed;
|
||||
break;
|
||||
case QuantityFormat::Scientific:
|
||||
ss << std::scientific;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ss << std::setprecision(format.precision) << value;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
|
||||
{
|
||||
if (PyUnicode_Check(args)) {
|
||||
|
||||
@@ -51,21 +51,6 @@ public:
|
||||
|
||||
static std::string schemaTranslate(const Quantity& quant);
|
||||
|
||||
/**
|
||||
* Quantity to string. Optionally apply format
|
||||
* The string is a number in C locale (i.e. the decimal separator is always a dot)
|
||||
* Scientific notation (if needed).
|
||||
*/
|
||||
|
||||
/** Does NOT include unit */
|
||||
static std::string
|
||||
toNumber(const Quantity& quantity,
|
||||
const QuantityFormat& format = QuantityFormat(QuantityFormat::Default));
|
||||
|
||||
/** Does NOT include unit */
|
||||
static std::string
|
||||
toNumber(double value, const QuantityFormat& format = QuantityFormat(QuantityFormat::Default));
|
||||
|
||||
static double toDouble(PyObject* args, const Base::Unit& u = Base::Unit());
|
||||
|
||||
static void setDecimals(std::size_t);
|
||||
|
||||
@@ -217,5 +217,6 @@ PyObject* UnitsApi::sToNumber(PyObject* /*self*/, PyObject* args)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return Py::new_reference_to(Py::String(toNumber(value, qf)));
|
||||
const Quantity quantity {value};
|
||||
return Py::new_reference_to(Py::String(quantity.toNumber(qf)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user