00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_STRING_CONV_H 00024 #define O2SCL_STRING_CONV_H 00025 /** \file string_conv.h 00026 \brief Various string conversion functions 00027 */ 00028 00029 #include <iostream> 00030 #include <cmath> 00031 #include <string> 00032 #include <fstream> 00033 #include <sstream> 00034 #include <o2scl/err_hnd.h> 00035 #include <o2scl/lib_settings.h> 00036 #include <gsl/gsl_ieee_utils.h> 00037 00038 #ifndef DOXYGENP 00039 namespace o2scl { 00040 #endif 00041 00042 /** 00043 \brief Convert a pointer to a string 00044 00045 This uses an \c ostringstream to convert a pointer to a string 00046 and is architecture-dependent. 00047 */ 00048 std::string ptos(void *p); 00049 00050 /** \brief Convert an integer to a string 00051 */ 00052 std::string itos(int x); 00053 00054 /** \brief Convert a double to a string 00055 */ 00056 std::string dtos(double x, int prec=6, bool auto_prec=false); 00057 00058 /** \brief Returns the number of characters required to display the 00059 exponent of \c x in scientific mode 00060 00061 This usually returns 2 or 3, depending on whether or not the 00062 absolute magnitude of the exponent is greater than 100. 00063 */ 00064 size_t size_of_exponent(double x); 00065 00066 /** 00067 \brief Convert a double to a string using a specified format 00068 00069 \todo Add error checking to this function using \c if(strout << x) 00070 */ 00071 std::string dtos(double x, std::ostream &format); 00072 00073 /** 00074 \brief Convert a string to an integer 00075 00076 If this function fails it will call set_err() and return zero. 00077 */ 00078 int stoi(std::string s); 00079 00080 /** 00081 \brief Convert a string to a double 00082 00083 If this function fails it will call set_err() and return zero. 00084 */ 00085 double stod(std::string s); 00086 00087 /** 00088 \brief Convert a double to a Latex-like string 00089 00090 The parameter \c sigfigs is the number of significant figures 00091 to give in the string. The parameters \c ex_min and \c ex_max 00092 represent the base-10 logarithm of the smallest and largest 00093 numbers to be represented without the string 00094 \verbatim 00095 $\times 10^{\mathrm{ex}}$ 00096 \endverbatim 00097 where \c ex is the relevant exponent. The number zero is always 00098 converted to "0". 00099 00100 Note that this function does not warn the user if the number 00101 of significant figures requested is larger than the machine 00102 precision. 00103 00104 \todo Fix to ensure final zeros are printed properly 00105 if requested 00106 */ 00107 std::string double_to_latex(double x, int sigfigs=5, int ex_min=-2, 00108 int ex_max=3); 00109 00110 /** 00111 \brief Convert a double to a HTML-like string 00112 00113 This uses \c \× and \c <sup></sup> to convert a double 00114 to a string for use on the web. 00115 00116 The parameter \c sigfigs is the number of significant figures to 00117 give in the string. The parameters \c ex_min and \c ex_max 00118 represent the base-10 logarithm of the smallest and largest 00119 numbers to be represented without exponential notation. The 00120 number zero is always converted to "0". Superscripts are 00121 implemented using (can't use greater than size in documentation) 00122 00123 Note that this function does not warn the user if the number 00124 of significant figures requested is larger than the machine 00125 precision. 00126 */ 00127 std::string double_to_html(double x, int sigfigs=5, int ex_min=-2, 00128 int ex_max=3); 00129 00130 /** 00131 \brief Convert a double to a string containing IEEE representation 00132 00133 Modeled after the GSL function \c gsl_ieee_fprintf_double(), but 00134 converts to a \c string instead of a \c FILE *. 00135 */ 00136 std::string double_to_ieee_string(double *x); 00137 00138 #ifndef DOXYGENP 00139 } 00140 #endif 00141 00142 #endif 00143