1 /*
2  *
3  * Copyright (C) 2007,2008  Markko Merzin (markko.merzin@ut.ee)
4  *
5  * This file is part of ImageJ plugin Volumest.
6  * 
7  * Volumest plugin is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24/**
25 * Formats doubles.
26 * @author Markko Merzin, markko.merzin@ut.ee
27 */
28public class ResultsFormater {
29
30    public static String formatNumber(double num, int significantDigits) {
31
32        double rescaled;
33        if (num > 0.000000000001) {
34            int a = (int) Math.floor(Math.log10(num)) + 1 - significantDigits;
35
36            double toRounding = num / Math.pow(10, a);
37            double rounded = Math.round(toRounding);
38            rescaled = rounded * Math.pow(10, a);
39        } else {
40            rescaled = num;
41        }
42
43        String toReturn = Double.toString(rescaled);
44
45        if (toReturn.endsWith(".0")) {
46            toReturn = toReturn.substring(0, toReturn.length() - 2);
47        }
48
49        return toReturn;
50
51    }
52}
53