benjaminz
benjaminz

Reputation: 3238

C++: Converting a double/float to string, preserve scientific notation and precision

I'm trying to convert double/float numbers to string. Some of these numbers contain scientific notation "e", and I want to preserve it after the conversion. I searched through google and stackoverflow and didn't find a matching answer to my use case.

Here are some examples and expected output:

input: 1.7976931348623157e+308
output: "1.7976931348623157e+308"

Here is a full main.cc file that can be compiled:

#include <iostream>
#include <sstream>
#include <string>

template <typename T>
std::string ToString(T value) {
  std::stringstream out;
  out << std::fixed;
  out << value;
  return out.str();
}

int main () {
    double mydouble = 1.7976931348623157e+308;
    std::cout << ToString(mydouble) << std::endl;
}

--- Update 11:17AM -----

There is no more weird characters after running my standalone code above, but the output is a long string-formatted float:

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

Not the expected "1.7976931348623157e+308".

-------- Outdated ----------------

But the output is a weird string:

"179769313486231610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000ÍÍÍÍÍÍÍÍýýýý««««««««««««««««îþîþîþîþ"

Upvotes: 0

Views: 2549

Answers (2)

Jose Palma
Jose Palma

Reputation: 756

Your code is running fine. If you have crap values in the output maybe there is a stack corruption somewhere else.

#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>

template <typename T>
inline std::string ToString(T value) {
  std::stringstream out;
  out << std::fixed;  
  out << value;
  return out.str();
}

int main() {
  double mydouble = 1.7976931348623157e+308; 
  std::cout << boost::lexical_cast<std::string>(mydouble) << '\n'; 
  std::cout << ToString(mydouble) << '\n';
}

Output:

1.7976931348623157e+308 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

As it was pointed out by Marcus in previous comments you don't want to use std::fixed.

Upvotes: 1

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36483

Two things:

Fixed isn't what you want if you want scientific notation. It's the opposite.

when I compile

#include <iostream>
#include <sstream>

template <typename T>
inline std::string ToString(T value) {
    std::stringstream out;
    out << std::fixed;  
    out << value;
    return out.str();
}
int main()  
{
    double mydouble = 1.7976931348623157e+308; 
    std::cout << ToString(mydouble) << std::endl; 
}

I get

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

which is right, which means there's a bug in your C++ compiler or standard library.

Upvotes: 1

Related Questions