@Test public void getAsObject_negativeIntegerNumberConvertsCorrectly() { Double result = (Double) numberConverter.getAsObject(null, null, "-545"); assertEquals(-545, result, 0.000001); }
@Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || value.trim().length() == 0) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a value"); message.setDetail("No value provided"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } int nrOfCommasAndDots = value.replaceAll("[^.,]", "").length(); if (nrOfCommasAndDots > 1) { FacesMessage message = new FacesMessage(); message.setSummary("Please use . as comma and do not use grouping"); message.setDetail("Only one comma allowed"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } try { value = value.replace(',', '.'); return Double.valueOf(Double.parseDouble(value)); } catch (NumberFormatException e) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a numeric value"); message.setDetail("Provided value is not a double"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } }
NumberConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || value.trim().length() == 0) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a value"); message.setDetail("No value provided"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } int nrOfCommasAndDots = value.replaceAll("[^.,]", "").length(); if (nrOfCommasAndDots > 1) { FacesMessage message = new FacesMessage(); message.setSummary("Please use . as comma and do not use grouping"); message.setDetail("Only one comma allowed"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } try { value = value.replace(',', '.'); return Double.valueOf(Double.parseDouble(value)); } catch (NumberFormatException e) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a numeric value"); message.setDetail("Provided value is not a double"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } } }
NumberConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || value.trim().length() == 0) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a value"); message.setDetail("No value provided"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } int nrOfCommasAndDots = value.replaceAll("[^.,]", "").length(); if (nrOfCommasAndDots > 1) { FacesMessage message = new FacesMessage(); message.setSummary("Please use . as comma and do not use grouping"); message.setDetail("Only one comma allowed"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } try { value = value.replace(',', '.'); return Double.valueOf(Double.parseDouble(value)); } catch (NumberFormatException e) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a numeric value"); message.setDetail("Provided value is not a double"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } }  }
NumberConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || value.trim().length() == 0) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a value"); message.setDetail("No value provided"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } int nrOfCommasAndDots = value.replaceAll("[^.,]", "").length(); if (nrOfCommasAndDots > 1) { FacesMessage message = new FacesMessage(); message.setSummary("Please use . as comma and do not use grouping"); message.setDetail("Only one comma allowed"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } try { value = value.replace(',', '.'); return Double.valueOf(Double.parseDouble(value)); } catch (NumberFormatException e) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a numeric value"); message.setDetail("Provided value is not a double"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } }  @Override Object getAsObject(FacesContext context, UIComponent component, String value); @Override String getAsString(FacesContext context, UIComponent component, Object value); }
NumberConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || value.trim().length() == 0) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a value"); message.setDetail("No value provided"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } int nrOfCommasAndDots = value.replaceAll("[^.,]", "").length(); if (nrOfCommasAndDots > 1) { FacesMessage message = new FacesMessage(); message.setSummary("Please use . as comma and do not use grouping"); message.setDetail("Only one comma allowed"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } try { value = value.replace(',', '.'); return Double.valueOf(Double.parseDouble(value)); } catch (NumberFormatException e) { FacesMessage message = new FacesMessage(); message.setSummary("Please enter a numeric value"); message.setDetail("Provided value is not a double"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ConverterException(message); } }  @Override Object getAsObject(FacesContext context, UIComponent component, String value); @Override String getAsString(FacesContext context, UIComponent component, Object value);  }