2

I have a string like that "2.1648797E -05" and I need to format it to convert "0.00021648797"

Is there any solution to do this conversion

MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 1
    You should show the code you have tried so far, and say specifically what about it doesn't work (e.g. compiler error, output in wrong format, etc). That way we can see, for example, that you intend to output using, say, `iostreams`. – BoBTFish Jul 09 '13 at 11:22
  • Did you try [Decimal.parse()](http://msdn.microsoft.com/en-us/library/system.decimal.parse(v=vs.71).aspx) ? – AllTooSir Jul 09 '13 at 11:23
  • 2
    @TheNewIdiot No suggestion that he(?) is on Windows, so I think we should stick to the Standard Library unless told otherwise. – BoBTFish Jul 09 '13 at 11:24
  • Do you like a solution in C ?? – Grijesh Chauhan Jul 09 '13 at 11:31
  • works fine with Decimal.parse() thks – MRebai Jul 09 '13 at 13:41

5 Answers5

2

try to use double or long long

cout << setiosflags(ios::fixed) << thefloat << endl;

An important characteristic of floating point is that they do not have precision associated with all the significant figures back to the decimal point for large values. The "scientific" display reasonably reflects the inherent internal storage realities.

user9371102
  • 1,278
  • 3
  • 21
  • 43
0

In C++ you can use std::stringstream First print the number, then read it as double and then print it using format specifiers to set the accuracy of the number to 12 digits. Take a look at this question for how to print decimal number with fixed precision.

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

If you are really just going from string representation to string representation and precision is very important or values may leave the valid range for doubles then I would avoid converting to a double. Your value may get altered by that due to precision errors or range problems.

Try writing a simple text parser. Roughly like that: Read the digits, omitting the decimal point up to the 'E' but store the decimal point position. After the 'E' read the exponent as a number and add that to your stored decimal position. Then output the digits again properly appending zeros at beginning or end and inserting the decimal point.

Ole Dittmann
  • 1,764
  • 1
  • 14
  • 22
-1

thanks guys and i fix it using :

Decimal dec = Decimal.Parse(str, System.Globalization.NumberStyles.Any);
MRebai
  • 5,344
  • 3
  • 33
  • 52
-1

There are unclear issues here
1. Was the space in "2.1648797E -05" intended, let's assume it is OK.
2. 2.1648797E-05 is 10 times smaller than 0.00021648797. Assume OP meant "0.000021648797" (another zero).
3. Windows is not tagged, but OP posted a Windows answer.

The major challenge here, and I think is the OP's core question is that std::precision() has different meanings in fixed versus default and the OP wants the default meaning in fixed.

Precision field differs between fixed and default floating-point notation. On default, the precision field specifies the maximum number of useful digits to display both before and after the decimal point, possible using scientific notation, while in fixed, the precision field specifies exactly how many digits to display after the decimal point.


2 approaches to solve this: Change the input string to a number and then output the number in the new fixed space format - that is presented below. 2nd method is to parse the input string and form the new format - not done here.

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
#include <cfloat>

double ConvertStringWithSpaceToDouble(std::string s) {
  // Get rid of pesky space in "2.1648797E -05"
  s.erase (std::remove (s.begin(), s.end(), ' '), s.end());
  std::istringstream i(s);
  double x;
  if (!(i >> x)) {
    x = 0; // handle error;
  }
  std::cout << x << std::endl;
  return x;
}

std::string ConvertDoubleToString(double x) {
  std::ostringstream s;
  double fraction = fabs(modf(x, &x));
  s.precision(0);
  s.setf(std::ios::fixed);
  // stream whole number part
  s << x << '.';
  // Threshold becomes non-zero once a non-zero digit found.
  // Its level increases with each additional digit streamed to prevent excess trailing zeros.
  double threshold = 0.0;
  while (fraction > threshold) {
    double digit;
    fraction = modf(fraction*10, &digit);
    s << digit;
    if (threshold) {
      threshold *= 10.0;
    }
    else if (digit > 0) {
      // Use DBL_DIG to define number of interesting digits
      threshold = pow(10, -DBL_DIG);
    }
  }
  return s.str();
}

int main(int argc, char* argv[]){
  std::string s("2.1648797E -05");
  double x = ConvertStringWithSpaceToDouble(s);
  s = ConvertDoubleToString(x);
  std::cout << s << std::endl;
  return 0;
  }
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256