All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.fmt.ABcdFmtPattern Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: ABcdFmtPattern.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * In order to parse numbers, ABcdFmtDefault is used. But formatting is done
 * using java.text.DecimalFormat which allows to use patterns and has some
 * more capabilities than ABcdFmtDefault.
 * 
 * Warning: Numbers with more than 15 digits are not processed correctly since DecimalFormat
 * internally uses doubles. 
 * 
 * @author YSD, 18.02.2004
 */
public class ABcdFmtPattern extends ABcdFmtDefault {
    
    /**
     * The internally stored DecimalFormat
     */
    private DecimalFormat           df_;
    
    
    /**
     * Constructor
     * 
     * @param pattern as defined in java.text.DecimalFormat
     * @param maxBeforeC max number of digits before the comma or -1 if unrestricted. Must not be zero.
     * @param maxAfterC  max number of digits after the comma or -1 if unrestricted
     * @param style may be DEFAULT, MANDATORY, NO_THOUS_SEPS, THOUS_SEPS, NO_NEG or ROUND_FRACTION. 
     *               Either NO_THOUS_SEPS or THOUS_SEPS may be specified.
     * @param l       a Locale
     */
    public ABcdFmtPattern (String pattern, int maxBeforeC, int maxAfterC, int style, Locale l) {
        super (maxBeforeC, maxAfterC, style, l);
        df_ = new DecimalFormat (pattern, new DecimalFormatSymbols (l));
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#format(java.lang.String)
     */
    public String format (String internal) {
        if (internal == null || internal.length() == 0) return "";
        return df_.format(new BigDecimal(internal).doubleValue());        
    }

    
//    /**
//     * Test method
//     */
//    public static void main (String[] args) {
//        ABcdFmtPattern       f = new ABcdFmtPattern("000000", 5, 0, ABcdFmt.DEFAULT, Locale.getDefault());
//        String               toParse = "12345";
//        System.out.println ("parse yields: " + f.parse(toParse));
//        System.out.println ("parse/format: " + f.format(f.parse(toParse)));
//    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy