Convert Double to string issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    Convert Double to string issues

    I have a double of unknown length that I need to split at the
    decimal. I thought I would convert it either to a string or a char.
    char seems to be the best since it easily lends itself to splitting.
    I have a few issues with this theory. Hard codeing char M[6] works
    fine for my test double -0.5 But I would like it to be more
    versitile. The format "%2.2f" works fine with my test data, but that
    should also be versitile.


    inline void LineSegment::pe rpBisect(const double m, const Point p)
    char M[6] ;
    sprintf(M,"%2.2 f",m);
  • kasthurirangan.balaji@gmail.com

    #2
    Re: Convert Double to string issues

    On Mar 28, 9:08 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
    I have a double of unknown length that I need to split at the
    decimal. I thought I would convert it either to a string or a char.
    char seems to be the best since it easily lends itself to splitting.
    I have a few issues with this theory. Hard codeing char M[6] works
    fine for my test double -0.5 But I would like it to be more
    versitile. The format "%2.2f" works fine with my test data, but that
    should also be versitile.
    >
    inline void LineSegment::pe rpBisect(const double m, const Point p)
    char M[6] ;
    sprintf(M,"%2.2 f",m);
    you can use stringstream for converting double to string. To find the
    digits after decimal, you may then use the string functions(find and
    substr).

    Thanks,
    Balaji.

    Comment

    • Jim Langston

      #3
      Re: Convert Double to string issues

      yogi_bear_79 wrote:
      I have a double of unknown length that I need to split at the
      decimal. I thought I would convert it either to a string or a char.
      char seems to be the best since it easily lends itself to splitting.
      I have a few issues with this theory. Hard codeing char M[6] works
      fine for my test double -0.5 But I would like it to be more
      versitile. The format "%2.2f" works fine with my test data, but that
      should also be versitile.
      >
      >
      inline void LineSegment::pe rpBisect(const double m, const Point p)
      char M[6] ;
      sprintf(M,"%2.2 f",m);
      Here is a way using stringstream. Depending on what you need to do with the
      parts depends on how you would optimize it if necessary.

      Output is:
      Whole Number: 1 Fraction: 2345
      Whole Number: 2 Fraction: 0
      Whole Number: 0 Fraction: 333333

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

      void ShowDouble( const double& Value )
      {
      std::stringstre am Stream;
      Stream << Value;
      std::string Digits;
      Stream >Digits;

      std::string::si ze_type Pos = Digits.find('.' );
      std::string WholeNumber;
      std::string Fraction;
      if ( Pos == std::string::np os )
      {
      WholeNumber = Digits;
      Fraction = "0";
      }
      else
      {
      WholeNumber = Digits.substr( 0, Pos );
      Fraction = Digits.substr( Pos + 1 );
      }

      std::cout << "Whole Number: " << WholeNumber << " Fraction: " <<
      Fraction << "\n";
      }

      int main()
      {
      ShowDouble( 1.2345 );
      ShowDouble( 2 );
      ShowDouble( 1.0 / 3 );
      }


      --
      Jim Langston
      tazmaster@rocke tmail.com


      Comment

      Working...