]>
Dogcows Code - chaz/yoink/blob - src/stlplus/strings/string_float.cpp
1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "string_float.hpp"
18 // added as a local copy to break the dependency on the portability library
19 static std::string
local_dformat(const char* format
, ...) throw(std::invalid_argument
)
21 std::string formatted
;
23 va_start(args
, format
);
27 for(int buffer_length
= 256; ; buffer_length
*=2)
29 buffer
= (char*)malloc(buffer_length
);
30 if (!buffer
) throw std::invalid_argument("string_float");
31 length
= _vsnprintf(buffer
, buffer_length
-1, format
, args
);
35 formatted
+= std::string(buffer
);
43 int length
= vasprintf(&buffer
, format
, args
);
44 if (!buffer
) throw std::invalid_argument("string_float");
46 formatted
+= std::string(buffer
);
50 if (length
< 0) throw std::invalid_argument("string_float");
54 ////////////////////////////////////////////////////////////////////////////////
55 // floating-point types
57 std::string
float_to_string(float f
, real_display_t display
, unsigned width
, unsigned precision
)
58 throw(std::invalid_argument
)
60 return double_to_string((double)f
, display
, width
, precision
);
63 std::string
double_to_string(double f
, real_display_t display
, unsigned width
, unsigned precision
)
64 throw(std::invalid_argument
)
69 return local_dformat("%*.*f", width
, precision
, f
);
70 case display_floating
:
71 return local_dformat("%*.*e", width
, precision
, f
);
73 return local_dformat("%*.*g", width
, precision
, f
);
75 throw std::invalid_argument("invalid radix display value");
79 ////////////////////////////////////////////////////////////////////////////////
81 float string_to_float(const std::string
& value
)
82 throw(std::invalid_argument
)
84 return (float)string_to_double(value
);
87 double string_to_double(const std::string
& value
)
88 throw(std::invalid_argument
)
90 // TODO - error checking
91 return strtod(value
.c_str(), 0);
94 ////////////////////////////////////////////////////////////////////////////////
96 } // end namespace stlplus
This page took 0.035458 seconds and 4 git commands to generate.