-1

This question is very clearly in no way an "exact duplicate" of the marked question, and that post does not address my goal.

If I have:

double yuge = 1e16;

And I try to add it to a string like this:

std::string boogers = std::to_string (yuge) + ".csv";

I get a file name of 100000000000000000.csv.

I want a nice compact version like 1e16.csv.

As you can see, I would like to use it as a file name, so output methods arent helpful. Halp! Thanks.

Job Guidos
  • 11
  • 1
  • 6
  • 1
    Since when there's a `operator+(double, char*)`? Show the actual code you use to accomplish this instead of nonsense. – StoryTeller - Unslander Monica Aug 03 '17 at 06:36
  • Since when there's a calm down sir. You obviously knew what I meant. Fixed. – Job Guidos Aug 03 '17 at 06:39
  • 1
    No I did not know what you meant. It's up to you to explain in full your problem. This is not a guessing game. – StoryTeller - Unslander Monica Aug 03 '17 at 06:40
  • 2
    @JobGuidos You have been a member for well over a year. By now you should know [how to ask good questions](http://stackoverflow.com/help/how-to-ask) and how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 03 '17 at 06:41
  • I suggest you police yourself instead of others. Would make it easier to get help. – StoryTeller - Unslander Monica Aug 03 '17 at 06:42
  • With attitude like yours no one would want to help you. Main rule of site is to provide code that compiles or a minimal example that shows error during compilation: https://stackoverflow.com/help/mcve – Swift - Friday Pie Aug 03 '17 at 06:49
  • Comments don't give "points". https://stackoverflow.com/help/whats-reputation – kaineub Aug 03 '17 at 06:57
  • @Job, if you'd bothered to read the documentation for `std::to_string(double)`, you'd see that it formats using fixed-width conversion, as `std::sprintf(buf, "%f", value)` would produce for sufficiently large `buf`. There's no `to_string()` equivalent to `std::sprintf(buf, "%e", value)`, so you'll have to write your own. – Toby Speight Aug 03 '17 at 08:01
  • Additionally, "output methods" *are* helpful - if you write to a `std::ostringstream`, for example. – Toby Speight Aug 03 '17 at 08:04

1 Answers1

1

you can use std::stringstream to construct the string instead of + operator.

#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>

int main()
{
  double youge = 1e16;
  std::stringstream ss;
  ss<<youge<<".csv";
  std::string filename = ss.str(); // filename = 1e+16.csv
  filename.erase(std::remove(filename.begin(), filename.end(), '+'), filename.end());// removing the '+' sign
  std::cout<<filename; // 1e16.csv
}
  • This is a really surprising solution. I would never have thought to even check if streamstream arbitrarily stores numbers as scientific notation. Thank you. Perfect answer. You can't see my upvote, but I did. – Job Guidos Aug 03 '17 at 07:17
  • It apparently does it for doubles >=1e6( [link](http://cpp.sh/5gtsw)) In other cases, perhaps `ss< – Przemysław Czechowski Aug 03 '17 at 07:33
  • Using that suggestion spews a lot of zeros, but [this](http://cpp.sh/5ddtp) seem to work as long as I don't use it for something like 1.903e5 which would yield 193e5. Fortunately my numbers happen to be bigger than 1e6 right now and simple. if anyone has another clean solution post another answer. Maybe something in a math or science library? Thanks. – Job Guidos Aug 03 '17 at 08:09