List of usage examples for org.apache.commons.math.complex ComplexFormat ComplexFormat
public ComplexFormat(String imaginaryCharacter)
From source file:org.sakaiproject.tool.assessment.jsf.validator.FinQuestionValidator.java
static boolean isComplexNumber(String value) { boolean isComplex = true; Complex complex = null;//from w ww. jav a 2s .c o m try { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); df.setGroupingUsed(false); // Numerical format ###.## (decimal symbol is the point) ComplexFormat complexFormat = new ComplexFormat(df); complex = complexFormat.parse(value); // This is because there is a bug parsing complex number. 9i is parsed as 9 if (complex.getImaginary() == 0 && value.contains("i")) isComplex = false; } catch (Exception e) { isComplex = false; } return isComplex; }
From source file:org.sakaiproject.tool.assessment.services.GradingService.java
/** * Validate a students numeric answer /* w ww .j av a 2 s.co m*/ * @param The answer to validate * @return a Map containing either Real or Complex answer keyed by {@link #ANSWER_TYPE_REAL} or {@link #ANSWER_TYPE_COMPLEX} */ public Map validate(String value) { HashMap map = new HashMap(); if (value == null || value.trim().equals("")) { return map; } String trimmedValue = value.trim(); boolean isComplex = true; boolean isRealNumber = true; BigDecimal studentAnswerReal = null; try { studentAnswerReal = new BigDecimal(trimmedValue); } catch (Exception e) { isRealNumber = false; } // Test for complex number only if it is not a BigDecimal Complex studentAnswerComplex = null; if (!isRealNumber) { try { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); df.setGroupingUsed(false); // Numerical format ###.## (decimal symbol is the point) ComplexFormat complexFormat = new ComplexFormat(df); studentAnswerComplex = complexFormat.parse(trimmedValue); // This is because there is a bug parsing complex number. 9i is parsed as 9 if (studentAnswerComplex.getImaginary() == 0 && trimmedValue.contains("i")) { isComplex = false; } } catch (Exception e) { isComplex = false; } } Boolean isValid = isComplex || isRealNumber; if (!isValid) { throw new FinFormatException("Not a valid FIN Input. studentanswer=" + trimmedValue); } if (isRealNumber) { map.put(ANSWER_TYPE_REAL, studentAnswerReal); } else if (isComplex) { map.put(ANSWER_TYPE_COMPLEX, studentAnswerComplex); } return map; }