00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 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/lib_settings.h> 00035 #include <gsl/gsl_ieee_utils.h> 00036 00037 #ifndef DOXYGENP 00038 namespace o2scl { 00039 #endif 00040 00041 /** 00042 \brief Convert a pointer to a string 00043 00044 This uses an \c ostringstream to convert a pointer to a string 00045 and is architecture-dependent. 00046 */ 00047 std::string ptos(void *p); 00048 00049 /** \brief Convert an integer to a string 00050 */ 00051 std::string itos(int x); 00052 00053 /** 00054 \brief Convert a boolean value to a string 00055 00056 This returns \c "1" for true and \c "0" for false. 00057 */ 00058 std::string btos(bool b); 00059 00060 /** \brief Convert a double to a string 00061 */ 00062 std::string dtos(double x, int prec=6, bool auto_prec=false); 00063 00064 /** \brief Returns the number of characters required to display the 00065 exponent of \c x in scientific mode 00066 00067 This usually returns 2 or 3, depending on whether or not the 00068 absolute magnitude of the exponent is greater than 100. 00069 */ 00070 size_t size_of_exponent(double x); 00071 00072 /** 00073 \brief Convert a double to a string using a specified format 00074 */ 00075 std::string dtos(double x, std::ostream &format); 00076 00077 /** 00078 \brief Convert a string to an integer 00079 00080 If this function fails it will call set_err() and return zero. 00081 */ 00082 int stoi(std::string s); 00083 00084 /** 00085 \brief Convert a string to a boolean value 00086 00087 This returns true only if the string has at least one character 00088 and the first non-whitespace character is either \c t, \c T, or 00089 one of the numbers 1 through 9. 00090 00091 This function never fails (it just returns false for an empty 00092 string). 00093 */ 00094 bool stob(std::string s); 00095 00096 /** 00097 \brief Convert a string to a double 00098 00099 If this function fails it will call set_err() and return zero. 00100 */ 00101 double stod(std::string s); 00102 00103 /** 00104 \brief Convert a double to a Latex-like string 00105 00106 The parameter \c sigfigs is the number of significant figures 00107 to give in the string. The parameters \c ex_min and \c ex_max 00108 represent the base-10 logarithm of the smallest and largest 00109 numbers to be represented without the string 00110 \verbatim 00111 $\times 10^{\mathrm{ex}}$ 00112 \endverbatim 00113 where \c ex is the relevant exponent. The number zero is always 00114 converted to "0". 00115 00116 If the parameter \c pad_zeros is true, this function 00117 adds zeros to the right side of the mantissa to guarantee that 00118 the requested number of significant digits is given. 00119 00120 Note that this function does not warn the user if the number 00121 of significant figures requested is larger than the machine 00122 precision. 00123 00124 \todo Consider converting to a class so the user can 00125 modify the strings used in giving the exponents, etc. 00126 */ 00127 std::string double_to_latex(double x, int sigfigs=5, int ex_min=-2, 00128 int ex_max=3, bool pad_zeros=false); 00129 00130 /** 00131 \brief Convert a double to a HTML-like string 00132 00133 This uses \c \× and \c <sup></sup> to convert a double 00134 to a string for use on the web. 00135 00136 The parameter \c sigfigs is the number of significant figures to 00137 give in the string. The parameters \c ex_min and \c ex_max 00138 represent the base-10 logarithm of the smallest and largest 00139 numbers to be represented without exponential notation. The 00140 number zero is always converted to "0". Superscripts are 00141 implemented using (can't use greater than size in documentation) 00142 00143 Note that this function does not warn the user if the number 00144 of significant figures requested is larger than the machine 00145 precision. 00146 00147 \todo Add a pad_zeros parameter as in double_to_latex(). 00148 */ 00149 std::string double_to_html(double x, int sigfigs=5, int ex_min=-2, 00150 int ex_max=3); 00151 00152 /** 00153 \brief Convert a double to a string containing IEEE representation 00154 00155 Modeled after the GSL function \c gsl_ieee_fprintf_double(), but 00156 converts to a \c string instead of a \c FILE *. 00157 */ 00158 std::string double_to_ieee_string(double *x); 00159 00160 /** 00161 \brief Find out if the number pointed to by \c x has a minus sign 00162 00163 This function returns true if the number pointed to by \c x has 00164 a minus sign using the GSL IEEE functions. It is useful, for 00165 example, in distinguishing "-0.0" from "+0.0". 00166 */ 00167 bool has_minus_sign(double *x); 00168 00169 #ifndef DOXYGENP 00170 } 00171 #endif 00172 00173 #endif 00174
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page