| 274 | // type T can be any number, e.g. int, qindex, float, double, long double |
| 275 | template <typename T> |
| 276 | string floatToStr(T num, bool hideSign=false, int overrideSigFigs=-1) { |
| 277 | |
| 278 | // write to stream (instead of calling to_string()) to auto-use scientific notation |
| 279 | std::stringstream buffer; |
| 280 | |
| 281 | // ensure +- is not shown if forced hidden |
| 282 | if (hideSign) { |
| 283 | buffer << std::noshowpos; |
| 284 | if (num < 0) |
| 285 | num *= -1; |
| 286 | } |
| 287 | |
| 288 | // impose user-set significant figures, unless caller overrode |
| 289 | int sigFigs = (overrideSigFigs != -1)? overrideSigFigs : global_maxNumPrintedSigFigs; |
| 290 | buffer << std::setprecision(sigFigs); |
| 291 | |
| 292 | // get string of e.g. 0, 1, 0.1, 1.2e-05, -1e+05 |
| 293 | buffer << num; |
| 294 | string out = buffer.str(); |
| 295 | |
| 296 | // remove superflous 0 prefix in scientific notation exponent |
| 297 | size_t ePos = out.find('e'); |
| 298 | if (ePos != string::npos && (out[ePos + 2] == '0')) |
| 299 | out.erase(ePos + 2, 1); |
| 300 | |
| 301 | // permit superflous + prefix of scientific notatio exponent, e.g. 2e+6 |
| 302 | |
| 303 | return out; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | // type T can be precision decimal (independent of qreal) i.e. float, double, long double |
no outgoing calls
no test coverage detected