|
-
July 15th, 2008, 04:20 PM
#1
problem about converting from string to long double
hi
i hope to get your help about this problem
i want to write Function that convert from string
as "$123,456,789,157.65"
to long double to use it in mathematical operations
-
July 15th, 2008, 04:27 PM
#2
Re: problem about converting from string to long double
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
July 15th, 2008, 04:47 PM
#3
Re: problem about converting from string to long double
Well, first you'd have to remove the commas. Then a simple sscanf() should do the trick with the %lg format specifier. Or you could try strtod() or atof()....just be careful about the dollar sign, whatever you do.
-
July 15th, 2008, 05:08 PM
#4
Re: problem about converting from string to long double
notice that its long double not double
this mean the number my be 19 digit so i can't use atof() or atod()
i oop with c++ book he saied i can use _atold() in math.h but no function like this in math.h
but first i must delete commas
i can do it by storing it in new char* and convert each number like this
long to double stold(char* str)
{
long double number=0.0;
char* s;
int n=0;
while(str[n]!=NULL)
{
if(str[n]>='0'||str[n]<='9'||str[n]=='.')
s[n]=str[n];
}//end while
for(int i=0;i<strlen(s);i++)
{
number+=static_cast<int>(s[n]-'0')*pow(10.0,i);
}
return number;
}
i don't know if this work or no
i want to know your openion
thnx for your help
-
July 15th, 2008, 09:22 PM
#5
Re: problem about converting from string to long double
If you're only expecting two decimal places (common with money), then you can easily read a 64-bit int (long long int) on the left of the decimal, and a standard int on the right. These use the %lld specifier. It's then trivial to add the left hand side to the right hand side over 100.0, and store the result in a long double.
Of course, you may wish to ensure the right hand side is really only two digits before you do the conversion.
In my opinion, though, once you start needing more than double precision, you're probably best off getting yourself an infinite precision library and using that.
Last edited by Lindley; July 15th, 2008 at 09:24 PM.
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
|