From 341e9d2bd0ca764c6cca060130a37b6f4ca8210d Mon Sep 17 00:00:00 2001 From: spx Date: Sat, 2 Jul 2011 10:16:38 +0000 Subject: [PATCH] Removed trailing zeros when DynamicVariables=1 even if a value is such as "1.01000". E.g. 1.00000 -> 1 1.01000 -> 1.01 1.01001 -> 1.01001 --- Library/Measure.cpp | 29 ++++++++++++++++++++++------- Library/Measure.h | 1 + Library/MeterWindow.cpp | 3 ++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 46abab47..f70ef43c 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -569,13 +569,8 @@ const WCHAR* CMeasure::GetStringValue(AUTOSCALE autoScale, double scale, int dec } else if (decimals == -1) { - _snwprintf_s(buffer, _TRUNCATE, L"%.5f", val); - - size_t len = wcslen(buffer); - if (len >= 6 && wcscmp(&buffer[len - 6], L".00000") == 0) - { - buffer[len - 6] = L'\0'; - } + int len = _snwprintf_s(buffer, _TRUNCATE, L"%.5f", val); + RemoveTrailingZero(buffer, len); } else { @@ -631,6 +626,26 @@ void CMeasure::GetScaledValue(AUTOSCALE autoScale, int decimals, double theValue _snwprintf_s(buffer, sizeInWords, _TRUNCATE, format, value); } +void CMeasure::RemoveTrailingZero(WCHAR* str, int strLen) +{ + --strLen; + while (strLen >= 0) + { + if (str[strLen] == L'0') + { + str[strLen] = L'\0'; + --strLen; + } + else + { + if (str[strLen] == L'.') + { + str[strLen] = L'\0'; + } + break; + } + } +} /* ** GetStats diff --git a/Library/Measure.h b/Library/Measure.h index 474ff765..5d2f8443 100644 --- a/Library/Measure.h +++ b/Library/Measure.h @@ -78,6 +78,7 @@ public: virtual const WCHAR* GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual); static void GetScaledValue(AUTOSCALE autoScale, int decimals, double theValue, WCHAR* buffer, size_t sizeInWords); + static void RemoveTrailingZero(WCHAR* str, int strLen); static CMeasure* Create(const WCHAR* measure, CMeterWindow* meterWindow, const WCHAR* name); diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index 0483f536..16afe2a0 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -983,7 +983,8 @@ void CMeterWindow::RunBang(BANGCOMMAND bang, const WCHAR* arg) if (result != -1) { WCHAR buffer[256]; - _snwprintf_s(buffer, _TRUNCATE, L"%f", value); + int len = _snwprintf_s(buffer, _TRUNCATE, L"%.5f", value); + CMeasure::RemoveTrailingZero(buffer, len); const std::wstring& resultString = buffer;