From f7b9606639a2c3a7836e35472fe3c5946e55ba3e Mon Sep 17 00:00:00 2001 From: John Scipione Date: Fri, 6 Apr 2012 16:50:27 -0400 Subject: [PATCH] Patch by ahenriksson to fix a computation bug in Deskcalc Ticket #8389 Signed off by John Scipione When trimming trailing 0's to make the number fit in the window, make sure to only trim trailing 0's AFTER the decimal point, not before. --- src/apps/deskcalc/ExpressionTextView.cpp | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/apps/deskcalc/ExpressionTextView.cpp b/src/apps/deskcalc/ExpressionTextView.cpp index 1c59b5488b..c6b36ecd55 100644 --- a/src/apps/deskcalc/ExpressionTextView.cpp +++ b/src/apps/deskcalc/ExpressionTextView.cpp @@ -258,7 +258,7 @@ ExpressionTextView::SetValue(BString value) stringWidth = font.StringWidth(value); } - // there is no need to keep the period if no digits follow + // no need to keep the period if no digits follow if (value[offset] == '.') { value.Remove(offset, 1); offset--; @@ -273,7 +273,7 @@ ExpressionTextView::SetValue(BString value) digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1 if (digit != 10) break; - value.Remove(offset, 1); + value[offset] = '0'; } if (digit == 10) { // carry over, shift the result @@ -296,16 +296,26 @@ ExpressionTextView::SetValue(BString value) } else { // increase the current digit value with one value[offset] = char(digit + 48); + + // set offset to last digit + offset = value.FindFirst('E'); + if (offset == B_ERROR) + offset = value.CountChars(); + offset--; } } - // remove trailing zeros - while (value[offset] == '0') - value.Remove(offset--, 1); + // clean up decimal part if we have one + if (value.FindFirst('.') != B_ERROR) { + // remove trailing zeros + while (value[offset] == '0') + value.Remove(offset--, 1); - // there is no need to keep the period if no digits follow - if (value[offset] == '.') - value.Remove(offset, 1); + // there is no need to keep the period if no + // digits follow + if (value[offset] == '.') + value.Remove(offset, 1); + } } // set the new value