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

View File

@ -16,6 +16,7 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.grid.ed; package org.compiere.grid.ed;
import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.ParseException; import java.text.ParseException;
@ -88,6 +89,8 @@ public final class MDocNumber extends PlainDocument
private char m_groupingSeparator = ','; private char m_groupingSeparator = ',';
/** Minus Sign */ /** Minus Sign */
private char m_minusSign = '-'; private char m_minusSign = '-';
/** Percent Sign */
private char m_percentSign = '%';
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger (MDocNumber.class); private static CLogger log = CLogger.getCLogger (MDocNumber.class);
@ -191,7 +194,7 @@ public final class MDocNumber extends PlainDocument
} // decimal or thousand } // decimal or thousand
// something else // 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 // Minus - put minus on start of string
@ -208,6 +211,35 @@ public final class MDocNumber extends PlainDocument
{ {
log.fine("Input=" + c + " (" + (int)c + ")"); 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(), String result = VNumber.startCalculator(m_tc, getText(),
m_format, m_displayType, m_title, c); m_format, m_displayType, m_title, c);
super.remove(0, content.length()); super.remove(0, content.length());
@ -216,6 +248,7 @@ public final class MDocNumber extends PlainDocument
m_tc.setText(result); m_tc.setText(result);
} }
} }
}
else else
ADialog.beep(); ADialog.beep();
} // insertString } // insertString

View File

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