The calculator tool is calculating 100*15% as 1500 instead of 15. - thanks to Michael McKay

Link to SF Tracker: http://sourceforge.net/support/tracker.php?aid=3051863
This commit is contained in:
Redhuan D. Oon 2011-03-26 00:15:58 -05:00
parent 4fe8c7dfaa
commit 856357f4f5
3 changed files with 51 additions and 11 deletions

View File

@ -484,6 +484,11 @@ public final class Calculator extends CDialog
// get operand
char op = token.charAt(0);
if (op == '%') {
firstNo = firstNo.divide(new BigDecimal(100.0), m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
m_number = firstNo;
}
// no second number
if (!st.hasMoreTokens())
return m_number;
@ -502,7 +507,7 @@ public final class Calculator extends CDialog
}
BigDecimal secondNo = new BigDecimal(secondNumber.toString());
// Check the next operant
// Check the next operand
char op2 = 0;
if (st.hasMoreTokens())
{
@ -518,8 +523,7 @@ public final class Calculator extends CDialog
// Percent operation
if (op2 == '%')
secondNo = firstNo.multiply(secondNo)
.divide(new BigDecimal(100.0), m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
secondNo = secondNo.divide(new BigDecimal(100.0), m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
switch (op)
{
@ -650,6 +654,9 @@ public final class Calculator extends CDialog
input = 'C';
else if (code == KeyEvent.VK_ENTER)
input = '=';
else if (code == KeyEvent.VK_SHIFT)
// ignore
return;
// abort
else if (code == KeyEvent.VK_CANCEL || code == KeyEvent.VK_ESCAPE)
{

View File

@ -16,6 +16,7 @@
*****************************************************************************/
package org.compiere.grid.ed;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
@ -88,6 +89,8 @@ public final class MDocNumber extends PlainDocument
private char m_groupingSeparator = ',';
/** Minus Sign */
private char m_minusSign = '-';
/** Percent Sign */
private char m_percentSign = '%';
/** Logger */
private static CLogger log = CLogger.getCLogger (MDocNumber.class);
@ -191,7 +194,7 @@ public final class MDocNumber extends PlainDocument
} // decimal or thousand
// something else
else if (VNumber.AUTO_POPUP || "=+-/*".indexOf(c) > -1)
else if (VNumber.AUTO_POPUP || "=+-/*%".indexOf(c) > -1)
{
// Minus - put minus on start of string
@ -208,6 +211,35 @@ public final class MDocNumber extends PlainDocument
{
log.fine("Input=" + c + " (" + (int)c + ")");
if (c == m_percentSign && offset > 0 ) {
// don't convert integers to percent. 1% = 0?
if (m_displayType == DisplayType.Integer)
return;
// divide by 100
else
{
String value = getText();
BigDecimal percentValue = new BigDecimal(0.0);
try
{
if (value != null && value.length() > 0)
{
Number number = m_format.parse(value);
percentValue = new BigDecimal (number.toString());
percentValue = percentValue.divide(new BigDecimal(100.0), m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
m_tc.setText(m_format.format(percentValue));
}
}
catch (ParseException pe)
{
log.info("InvalidEntry - " + pe.getMessage());
}
}
}
else
{
String result = VNumber.startCalculator(m_tc, getText(),
m_format, m_displayType, m_title, c);
super.remove(0, content.length());
@ -216,6 +248,7 @@ public final class MDocNumber extends PlainDocument
m_tc.setText(result);
}
}
}
else
ADialog.beep();
} // insertString

View File

@ -735,7 +735,7 @@ public final class VNumber extends JComponent
// Actual Call
Calculator calc = new Calculator(frame, title,
displayType, format, startValue);
if ( "*+-/".indexOf(operator) > -1 )
if ( "*+-/%".indexOf(operator) > -1 )
calc.handleInput(operator);
AEnv.showCenterWindow(frame, calc);
BigDecimal result = calc.getNumber();