CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: coversion

  1. #1
    Join Date
    Nov 2009
    Posts
    128

    Question coversion

    my problem is to get number of decimals digits after the decimal in a number of type double.So i have written below code.but it displays the ERROR which is


    cannot convert parameter 1 from 'std::string' to 'char *'

    Code:
    string s;
     string t;
     wsprintf(s, "%g" ,error_bound);
     t = s.substr(s.find(".")+1);
    
     return t.length();
    where did i go wrong ?
    pls help.
    Last edited by dskp; October 4th, 2011 at 01:55 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: coversion


  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: coversion

    Quote Originally Posted by dskp View Post
    my problem is to get number of decimals digits after the decimal in a number of type double.
    If you use a stringstream, you can call precision to set the number of decimals.
    Quote Originally Posted by dskp View Post
    cannot convert parameter 1 from 'std::string' to 'char *'

    Code:
    string s;
     string t;
     wsprintf(s, "%g" ,error_bound);
     t = s.substr(s.find(".")+1);
    
     return t.length();
    where did i go wrong ?
    pls help.
    You cannot write to a string like this. If you really want to use the printf function, then you can use a std::vector<char> as a buffer. However, I recommend just using a stringstream.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: coversion

    Quote Originally Posted by dskp View Post
    my problem is to get number of decimals digits after the decimal in a number of type double.
    This is not a well-defined problem in general. Some real numbers may have an infinite number of "digits after the decimal", such as PI or 1/3. Of course, in floating point only a limited number of digits is supported.

    Furthermore, because floating point is not exact, even a value which *should* have a finite number of digits after the decimal, like 2.0, may actually be represented by 1.999999999999999999999999.

    Therefore, "digits after the decimal" is purely a matter of output formatting. When outputting the value, you instruct the program how many digits to round off to. You should not think of it as an inherent property of the double value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured