I wrote the following code for converting double to string.I was not supposed to use sprintf or ostream . The output is quite erratic.
The list of input with corresponding output :
- 2.0 2.0
- 2.5 2.5
- -2.0 -2.0
- 2.987 2.9879947598364142621957397469375
- -2.987 -2.9879947598364142621957397469375
#include <iostream>
#include <math.h>
using namespace std;
string reverse(string input);
string myDtoA(double num);
string itoa(int num);
int main()
{
double inp=-2.987;
cout<<myDtoA(inp)<<endl;
}
string myDtoA(double num)
{
if(num>0)
{
int inPart;
double intPart,fractPart;
fractPart = modf(num,&intPart);
inPart=(int)intPart;
string ret;
ret = itoa(inPart);
if(fractPart!=0)
{
ret.append(".");
double ceilOfFraction = ceil(fractPart);
while(ceilOfFraction !=0)
{
double inP,fP;
fractPart*=10;
fP=modf(fractPart,&inP);
int a =(int)inP;
ret.append(itoa(a));
fractPart=fP;
ceilOfFraction = ceil(fractPart);
}
}
else
{ret.append(".0");}
return ret;
}
else if(num==0)
{
return "0";
}
else if(num<0)
{
string ret = "-";
ret.append(myDtoA(-num));
return ret;
}
}
string itoa(int num)
{
char* str = new char[120];
int i=0;
// Process individual digits
while (num != 0)
{
int rem = num % 10;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/10;
}
string ret(str);
return reverse(ret);
}
/* A utility function to reverse a string */
string reverse(string input)
{
return std::string(input.rbegin(), input.rend());
}