Java Code Examples for java.text.DecimalFormat.setMaximumIntegerDigits()

The following are Jave code examples for showing how to use setMaximumIntegerDigits() of the java.text.DecimalFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: util4j   File: TestDecimalFormat.java   Source Code and License Vote up 6 votes
public static void test1(DecimalFormat df) {
	//默认显示3位小数
	double d = 1.5555555;
	System.out.println(df.format(d));//1.556
	//设置小数点后最大位数为5
	df.setMaximumFractionDigits(5);
	df.setMinimumIntegerDigits(15);
	System.out.println(df.format(d));//1.55556
	df.setMaximumFractionDigits(2);
	System.out.println(df.format(d));//1.56
	//设置小数点后最小位数,不够的时候补0
	df.setMinimumFractionDigits(10);
	System.out.println(df.format(d));//1.5555555500
	//设置整数部分最小长度为3,不够的时候补0
	df.setMinimumIntegerDigits(3);
	System.out.println(df.format(d));
	//设置整数部分的最大值为2,当超过的时候会从个位数开始取相应的位数
	df.setMaximumIntegerDigits(2);
	System.out.println(df.format(d));
}
 
Example 2
Project: personium-core   File: PersoniumJsonWriter.java   Source Code and License Vote up 6 votes
/**
 * Double型の値を既定のフォーマットで整形する.
 * 情報落ちの起こらない範囲で固定小数点数表現に変換する
 * @param value 整形する値
 * @return 整形結果
 */
private String formatDoubleValue(double value) {
    // 固定小数表現に変換した文字列を生成する
    DecimalFormat format = new DecimalFormat("#.#");
    format.setMaximumIntegerDigits(MAX_INTEGER_DIGITS);
    format.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
    String fomattedValue = format.format(value);

    // 固定小数表現に変換した文字列を一度Double型に変換して
    // 情報落ちがある場合は元の値を返却する
    String result = fomattedValue;
    if (value != Double.parseDouble(fomattedValue)) {
        result = Double.toString(value);
    }
    return result;
}
 
Example 3
Project: jmt   File: CSVLogger.java   Source Code and License Vote up 5 votes
/**
 * Creates a new CSVLogger that writes to the given file
 * @param file the file we should write to
 * @param columns the columns for the log file
 * @param append true to append to an existing file. False to create a new file
 * @param colSep the column separator
 * @param digitSep the decimal digits separator
 */
public CSVLogger(File file, String[] columns, boolean append, String colSep, char digitSep) {
	this.file = file;
	this.initialized = false;
	this.append = append;
	this.columns = columns;
	this.colSep = colSep;
	
	DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
	dfs.setDecimalSeparator(digitSep);
	numberFormat = new DecimalFormat("#.#", dfs);
	numberFormat.setMaximumFractionDigits(340);
	numberFormat.setMaximumIntegerDigits(340);
}
 
Example 4
Project: oscm   File: PriceConverter.java   Source Code and License Vote up 5 votes
@Override
protected Format getFormat(String pattern, Locale locale) {
    DecimalFormat format = (DecimalFormat) super.getFormat(pattern,
            locale);
    format.setMaximumIntegerDigits(NUMBER_OF_INTEGER_PLACES);
    format.setMaximumFractionDigits(NUMBER_OF_DECIMAL_PLACES);
    // avoid lost precision due to parsing to double:
    format.setParseBigDecimal(true);
    return format;
}
 
Example 5
Project: QN-ACTR-Release   File: CSVLogger.java   Source Code and License Vote up 5 votes
/**
 * Creates a new CSVLogger that writes to the given file
 * @param file the file we should write to
 * @param columns the columns for the log file
 * @param append true to append to an existing file. False to create a new file
 * @param colSep the column separator
 * @param digitSep the decimal digits separator
 */
public CSVLogger(File file, String[] columns, boolean append, String colSep, char digitSep) {
	this.file = file;
	this.initialized = false;
	this.append = append;
	this.columns = columns;
	this.colSep = colSep;
	
	DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
	dfs.setDecimalSeparator(digitSep);
	numberFormat = new DecimalFormat("#.#", dfs);
	numberFormat.setMaximumFractionDigits(340);
	numberFormat.setMaximumIntegerDigits(340);
}