Here you can find the source of getScientificFormatter(int precision)
public static DecimalFormat getScientificFormatter(int precision)
//package com.java2s; /************************************************************************************** Copyright 2015 Applied Research Associates, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at://from w w w .jav a 2 s . c o m http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. **************************************************************************************/ import java.text.DecimalFormat; import java.util.*; public class Main { private static Map<Integer, DecimalFormat> formatters = new HashMap<Integer, DecimalFormat>(); private static Map<Integer, DecimalFormat> scientificFormatters = new HashMap<Integer, DecimalFormat>(); public static DecimalFormat getScientificFormatter(int precision) { DecimalFormat df = scientificFormatters.get(precision); if (df == null) { StringBuffer format = new StringBuffer("0."); for (int i = 0; i < precision; i++) format.append("#"); format.append("E0"); df = new DecimalFormat(format.toString()); scientificFormatters.put(precision, df); } return df; } public static String toString(double d, int precision) { // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly StringBuffer sb = new StringBuffer(); if (precision > 0) { DecimalFormat df = getFormatter(precision); sb.append(df.format(d)); } else { sb.append(d); } return sb.toString(); } public static String toString(double[] data) { return toString(data, -1); } public static String toString(double[] data, int precision) { // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly StringBuffer sb = new StringBuffer(); if (precision > 0) { DecimalFormat df = getFormatter(precision); for (int i = 0; i < data.length; i++) { sb.append(df.format(data[i])); if (i < data.length - 1) sb.append(","); } } else { for (int i = 0; i < data.length; i++) { sb.append(data[i]); if (i < data.length - 1) sb.append(","); } } return sb.toString(); } public static String toString(List<Double> data) { return toString(data, -1); } public static String toString(List<Double> data, int precision) { int idx = 0; // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly StringBuffer sb = new StringBuffer(); if (precision > 0) { DecimalFormat df = getFormatter(precision); for (Double d : data) { sb.append(df.format(d)); if (idx++ != data.size()) sb.append(","); } } else { for (Double d : data) { sb.append(d); if (idx++ != data.size()) sb.append(","); } } return sb.toString(); } public static DecimalFormat getFormatter(int precision) { DecimalFormat df = formatters.get(precision); if (df == null) { StringBuffer format = new StringBuffer("#0."); for (int i = 0; i < precision; i++) format.append("#"); df = new DecimalFormat(format.toString()); formatters.put(precision, df); } return df; } }