001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.utilities;
020
021import java.io.File;
022import java.text.DecimalFormat;
023
024import org.dllearner.utilities.statistics.Stat;
025
026public class StringFormatter {
027
028        
029        /**
030         * formats a double value between 0 and 100 to a percentage
031         * ex: 0.7854684  will be return 78.5%
032         * @param d the double value
033         */
034        public static String doubleToPercent(double d){
035                return doubleToPercent( d,  1,  true);
036        }
037        
038        public static String doubleToPercent(double d, int decimals){
039                
040                return doubleToPercent( d,  decimals,  true);
041                
042        }
043        
044        
045        /**
046         * used for regex difficult chars like _ or %
047         * @param s
048         * @param search
049         * @param replacement
050         * @return
051         */
052        public static String myReplaceAll(String s, char search, String replacement ){
053                String ret ="";
054                char[] arr = s.toCharArray();
055                for (char anArr : arr) {
056                        if (anArr == search) {
057                                ret += replacement;
058                        } else {
059                                ret += anArr;
060                        }
061                }
062                return ret;
063                
064        }
065        
066        
067        
068        public static String doubleToPercent(double d, int decimals, boolean addPercentSign){
069                
070                String format = (decimals==0)?"00":".";
071                for (int i = 0; i < decimals; i++) {
072                        format += "0";
073                }
074                format+="%";
075                DecimalFormat df = new DecimalFormat( format );
076                String ret = df.format(d);
077                ret = (addPercentSign)?ret:ret.replaceAll("%", "");
078                return ret;
079                
080        }
081        
082        public static String doubleRound(double d, int decimals, String before, String after){
083                String ret ="";
084                if(decimals==0){
085                        int retInt = (int) Math.floor((d+0.5));
086                        ret = retInt+"";
087                }else{
088                        String format = ".";
089                        for (int i = 0; i < decimals; i++) {
090                                format += "0";
091                        }
092                        
093                        DecimalFormat df = new DecimalFormat( format );
094                        ret = df.format(d);
095                        ret = ret.replaceAll("%", "");
096                }
097                ret = before + ret+ after;
098                return ret;
099                
100        }
101        
102        public static String convertStatPercentageToLatex(Stat s,
103                        int decimals,
104                        boolean addPercentSign,
105                        boolean includeSTDDeviation){
106                String ret ="";
107                
108                ret = doubleToPercent(s.getMean(), decimals, addPercentSign);
109                ret = ret.replaceAll("%", "\\%");
110                if(includeSTDDeviation){
111                        ret += " ($\\pm$"+doubleToPercent(s.getStandardDeviation(), decimals, false)+")";
112                }
113                return ret;
114        }
115        
116        public static String convertStatDoubleToLatex(Stat s,
117                        int decimals,
118                        boolean includeSTDDeviation){
119                return convertStatDoubleToLatex(s, decimals,"","",includeSTDDeviation);
120        }
121        
122        
123        public static String convertStatDoubleToLatex(Stat s,
124                        int decimals,
125                        String before,
126                        String after,
127                        boolean includeSTDDeviation){
128                String ret ="";
129                
130                ret = doubleRound(s.getMean(), decimals, before, after);
131        
132                if(includeSTDDeviation){
133                        ret += doubleRound(s.getStandardDeviation(), decimals," ($\\pm$", after+")" );
134                }
135                return ret;
136        }
137        
138        
139         public static boolean isWhitespace(String str) {
140              if (str == null) {
141                  return false;
142              }
143              int sz = str.length();
144              for (int i = 0; i < sz; i++) {
145                  if ((!Character.isWhitespace(str.charAt(i)))) {
146                      return false;
147                  }
148              }
149              return true;
150          }
151        
152        
153        public static void main(String[] args) {
154                double d = 0.55555;
155                System.out.println(doubleToPercent(d, 0));
156                System.out.println(doubleToPercent(d, 1));
157                System.out.println(doubleToPercent(d, 2));
158                System.out.println(doubleToPercent(d, 3));
159                System.out.println(doubleToPercent(d, 0, false));
160                System.out.println(doubleToPercent(d, 1, false));
161                System.out.println(doubleToPercent(d, 2, false));
162                System.out.println(doubleToPercent(d, 3, false));
163                d= 55.55555;
164                System.out.println(doubleRound(d, 0, "|", "|"));
165                System.out.println(doubleRound(d, 1, "|", "|"));
166                System.out.println(doubleRound(d, 2, "|", "|"));
167                System.out.println(doubleRound(d, 3, "|", "|"));
168        
169        }
170        
171        public static String checkIfDirEndsOnSlashAndRemove(String dir){
172                if(dir.endsWith(File.separator)){
173                        dir=dir.substring(0,dir.length()-File.separator.length());
174                }
175                return dir;
176        }
177        
178        
179}