📄 numberformatter.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2005/3/30
*
* $Author$
* $Date$
* $Revision$
*
*/
package eti.bi.util;
import java.text.DecimalFormat;
import eti.bi.common.data.MissingValue;
/**
* @author kelvinj
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class NumberFormatter {
public final static int MAX_FRACTION_DIGIT = 3;
public final static boolean ALWAYS_SHOWN_SEPARATOR = false;
public final static double FORMAT_THRESHOLD = 0.001;
private static DecimalFormat m_DecimalFormat = new DecimalFormat();
private static DecimalFormat m_ScientificFormat = new DecimalFormat();
private static String m_scientificPattern = "0.###E00";
static
{
m_DecimalFormat.setDecimalSeparatorAlwaysShown(ALWAYS_SHOWN_SEPARATOR);
m_DecimalFormat.setMaximumFractionDigits(MAX_FRACTION_DIGIT);
m_DecimalFormat.setGroupingUsed(false);
m_ScientificFormat.setDecimalSeparatorAlwaysShown(ALWAYS_SHOWN_SEPARATOR);
m_ScientificFormat.setMaximumFractionDigits(MAX_FRACTION_DIGIT);
m_ScientificFormat.applyPattern(m_scientificPattern);
}
/**
* Constructor
*/
public NumberFormatter() {
super();
}
public static String format(double a_Double)
{
if (Double.isNaN(a_Double))
{
return MissingValue.DISPLAY_VALUE;
}else if (a_Double<FORMAT_THRESHOLD && a_Double>0)
{
// If value is in between FORMAT_THRESHOLD and zero, use scientific representation
return m_ScientificFormat.format(a_Double);
}else
{
return m_DecimalFormat.format(a_Double);
}
}
public static String decimalFormat(double a_Double, int a_MaximumFractionDigits)
{
if (Double.isNaN(a_Double))
{
return MissingValue.DISPLAY_VALUE;
}
DecimalFormat aDecimalFormat = new DecimalFormat();
aDecimalFormat.setDecimalSeparatorAlwaysShown(false);
aDecimalFormat.setMaximumFractionDigits(a_MaximumFractionDigits);
aDecimalFormat.setGroupingUsed(false);
return aDecimalFormat.format(a_Double);
}
public static String format(int a_Int)
{
return String.valueOf(a_Int);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -