Java tutorial
/** * Copyright 2016 Red Sqirl, Ltd. All rights reserved. * Red Sqirl, Clarendon House, 34 Clarendon St., Dublin 2. Ireland * * This file is part of Red Sqirl * * User agrees that use of this software is governed by: * (1) the applicable user limitations and specified terms and conditions of * the license agreement which has been entered into with Red Sqirl; and * (2) the proprietary and restricted rights notices included in this software. * * WARNING: THE PROPRIETARY INFORMATION OF Red Sqirl IS PROTECTED BY IRISH AND * INTERNATIONAL LAW. UNAUTHORISED REPRODUCTION, DISTRIBUTION OR ANY PORTION * OF IT, MAY RESULT IN CIVIL AND/OR CRIMINAL PENALTIES. * * If you have received this software in error please contact Red Sqirl at * support@redsqirl.com */ package com.redsqirl.useful; import java.io.Serializable; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import org.apache.commons.lang3.math.NumberUtils; public class NumberConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return value; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null) { return null; } if (value instanceof Float || value instanceof Double) { double dblValue = Double.parseDouble(value.toString()); if (dblValue - ((int) dblValue) > 0) { if (dblValue > 10E5 || dblValue - ((int) dblValue) < 10E-3) { return String.format("%.2E", dblValue); } else { return String.format("%.2f", dblValue); } } } return value.toString(); } }