#include "pch.h" #include "NumberFormattingUtils.h" using namespace std; namespace CalcManager::NumberFormattingUtils { /// /// Trims out any trailing zeros or decimals in the given input string /// /// number to trim void TrimTrailingZeros(_Inout_ wstring& number) { if (number.find(L'.') == wstring::npos) { return; } wstring::iterator iter; for (iter = number.end() - 1;; iter--) { if (*iter != L'0') { number.erase(iter + 1, number.end()); break; } } if (*(number.end() - 1) == L'.') { number.erase(number.end() - 1, number.end()); } } /// /// Get number of digits (whole number part + decimal part) /// the number unsigned int GetNumberDigits(wstring value) { TrimTrailingZeros(value); unsigned int numberSignificantDigits = static_cast(value.size()); if (value.find(L'.') != wstring::npos) { --numberSignificantDigits; } if (value.find(L'-') != wstring::npos) { --numberSignificantDigits; } return numberSignificantDigits; } /// /// Get number of digits (whole number part only) /// the number unsigned int GetNumberDigitsWholeNumberPart(double value) { return value == 0 ? 1 : (1 + (int)log10(abs(value))); } /// /// Rounds the given double to the given number of significant digits /// /// input double /// int number of significant digits to round to wstring RoundSignificantDigits(double num, int numSignificant) { wstringstream out(wstringstream::out); out << fixed; out.precision(numSignificant); out << num; return out.str(); } /// /// Convert a Number to Scientific Notation /// /// number to convert wstring ToScientificNumber(double number) { wstringstream out(wstringstream::out); out << scientific << number; return out.str(); } }