/* PIP - Platform Independent Primitives Unit value Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include "piunits_value.h" #include "piliterals_string.h" #include "piunits_prefix.h" PIUnits::Value::Value(double v, int t) { m_value = v; m_class = Class::Internal::typeClasses.value(t); if (m_class) m_type = t; } PIString PIUnits::Value::toString(char format, int prec) const { if (isNotValid()) return Class::Internal::unknown; PIString ret; if (m_class->supportPrefixes(m_type)) { ret = Prefix::valueToString(m_value, m_class, m_type, format, prec); } else ret = m_class->valueToString(m_value, format, prec) + " "_a; ret += m_class->unit(m_type); return ret; } bool PIUnits::Value::convert(int type_to) { if (m_type == type_to) return true; auto * class_to = Class::Internal::typeClasses.value(type_to); if (!class_to) return false; if (m_class->classID() != class_to->classID()) return false; m_value = m_class->convert(m_value, m_type, type_to); m_type = type_to; return true; } PIUnits::Value PIUnits::Value::converted(int type_to) { Value ret(*this); if (!ret.convert(type_to)) return {}; return ret; }