/* * [DollarNumberFormatter.java] * * Summary: Dollar formatter for use in JSpinner. makes lead $ optional when keying. * * Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2007-08-15 initial version. */ package com.mindprod.spinner; import javax.swing.text.DefaultFormatter; import java.text.DecimalFormat; import java.text.ParseException; /** * Dollar formatter for use in JSpinner. makes lead $ optional when keying. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2007-08-15 initial version. * @since 2007-08-15 */ @SuppressWarnings( { "WeakerAccess" } ) public final class DollarNumberFormatter extends DefaultFormatter { /** * format for display and input of sale amount */ private static final DecimalFormat DOLLARFORMAT = new DecimalFormat( "'$'###,###,###,###,##0.00" ); /** * constructor */ public DollarNumberFormatter() { } /** * Remove all but 0-9 - and . from the string to elimate junk chars like $ comma, space. * * @param dirty raw string as keyed * * @return string without junk chars */ private String strip( String dirty ) { StringBuilder sb = new StringBuilder( dirty.length() ); for ( int i = 0; i < dirty.length(); i++ ) { char c = dirty.charAt( i ); switch ( c ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': case '-': sb.append( c ); break; default: // do nothing } } return sb.toString(); } /** * Converts the passed in String into an instance of getValueClass by way of the constructor that takes * a String argument. If getValueClass returns null, the class of the current value in the * JFormattedTextField will be used. If this is null, a String will be returned. If the constructor * thows an exception, a ParseException will be thrown. If there is no single argument String * constructor, string will be returned. * * @param string String to convert * * @return Object representation of text, namely Double to match what JSpinnerNumberModel uses. * @throws java.text.ParseException if there is an error in the conversion */ @SuppressWarnings( { "QuestionableName" } ) public Object stringToValue( String string ) throws ParseException { // strip the string of junk chars such as $ comma, space // extract double and round it to the nearest penny. // autobox the result into a Double. try { return Math.rint( Double.parseDouble( strip( string ) ) * 100 ) / 100; } catch ( NumberFormatException e ) { throw new ParseException( string, 0 ); } } /** * Converts the passed in Object into a String by way of the toString method. * * @param value Value to convert, some sort of Number * * @return String representation of value, padded with left zeroes to width. * @throws java.text.ParseException if there is an error in the conversion */ public String valueToString( Object value ) throws ParseException { return DOLLARFORMAT.format( value ); } }