|
-
October 4th, 2011, 01:50 AM
#1
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.
-
October 4th, 2011, 01:57 AM
#2
-
October 4th, 2011, 05:26 AM
#3
Re: coversion
 Originally Posted by dskp
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.
 Originally Posted by dskp
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
-
October 4th, 2011, 09:58 AM
#4
Re: coversion
 Originally Posted by dskp
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|