mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 05:43:10 -07:00
Merge 14c405f679
into be6eee0328
This commit is contained in:
commit
5aff858653
1 changed files with 7 additions and 2 deletions
|
@ -11,16 +11,18 @@ namespace UnitConversionManager::NumberFormattingUtils
|
||||||
/// <param name="number">number to trim</param>
|
/// <param name="number">number to trim</param>
|
||||||
void TrimTrailingZeros(_Inout_ wstring& number)
|
void TrimTrailingZeros(_Inout_ wstring& number)
|
||||||
{
|
{
|
||||||
|
//check if the number contains a decimal point.
|
||||||
if (number.find(L'.') == wstring::npos)
|
if (number.find(L'.') == wstring::npos)
|
||||||
{
|
{
|
||||||
return;
|
return; //no trailing zeros or decimal to trim.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//find the last non-zero digit and erase and triling zeros.
|
||||||
if (auto i = number.find_last_not_of(L'0'); i != wstring::npos)
|
if (auto i = number.find_last_not_of(L'0'); i != wstring::npos)
|
||||||
{
|
{
|
||||||
number.erase(number.cbegin() + i + 1, number.cend());
|
number.erase(number.cbegin() + i + 1, number.cend());
|
||||||
}
|
}
|
||||||
|
//remove the trailing decimal point if it exists.
|
||||||
if (number.back() == L'.')
|
if (number.back() == L'.')
|
||||||
{
|
{
|
||||||
number.pop_back();
|
number.pop_back();
|
||||||
|
@ -32,12 +34,15 @@ namespace UnitConversionManager::NumberFormattingUtils
|
||||||
/// <param name="value">the number</param>
|
/// <param name="value">the number</param>
|
||||||
unsigned int GetNumberDigits(wstring value)
|
unsigned int GetNumberDigits(wstring value)
|
||||||
{
|
{
|
||||||
|
//trim trailing zeros and decimal point.
|
||||||
TrimTrailingZeros(value);
|
TrimTrailingZeros(value);
|
||||||
unsigned int numberSignificantDigits = static_cast<unsigned int>(value.size());
|
unsigned int numberSignificantDigits = static_cast<unsigned int>(value.size());
|
||||||
|
//if the number contains a decimal point, reduce the count by one.
|
||||||
if (value.find(L'.') != wstring::npos)
|
if (value.find(L'.') != wstring::npos)
|
||||||
{
|
{
|
||||||
--numberSignificantDigits;
|
--numberSignificantDigits;
|
||||||
}
|
}
|
||||||
|
//if the number is negative, reduce the count by one
|
||||||
if (value.find(L'-') != wstring::npos)
|
if (value.find(L'-') != wstring::npos)
|
||||||
{
|
{
|
||||||
--numberSignificantDigits;
|
--numberSignificantDigits;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue