Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
int i;
i = (int) f;
//convert this integer into string ... and put a decimal ...
.
.
f = f - i; //now f contains the fractional part ...
f *=10;
i = (int) f; //this is first digit to the right of decimal ...
.
.
f = f-i;
f *= 10;
i = (int) f; // this is second digit to the right of decimal...
// repeate above steps four more times ...
Good solution based on higher level operations/abstractions like casting. +1. A little more formal treatment is below.
int i, j = 0;
i = (int) f;
f = f - i;
while(f > 0) {
f *= 10;
j = (j*10) + (int) f;
f = f - (int) f;
}
println atoi(i) + "." + atoi(j)
It might turn out to be a tough problem if casting is disallowed.
@Anonymous
The question is asking to focus you on a procedure for conversion and the conversion should not use a library function. However, in order to convert a number into a string of char literals, you ought to have an itoa() function, which you can always develop on your own.
there could be a problem :
suppose f=53.64
when i write code :
j is index after decimal part
while(rational>0)
{
int integer=(int)rational;
rational=rational-integer;
strin[j]=48+integer;
cout<<"thave value in decimal "<<j<<" is"<<strin[j]<<endl;
j++;
rational=rational*10;
}
i expect sollution to be 53.64
but actual result is :53.639999.....( where ... are digits till end of array) .
Now the question is how to round off 53.63999 to 53.64.. (Even the interview wants this too i guess, which is how float is represented in language )
is use of overloaded operator allowed ? if so in Java
String result = "" + <number value i.e 7.64>;
but it can not be this simple :)
I solved it in c++...Please let me know any improvements..
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#define MAXLEN 350
using namespace std;
void reverse(char * str , int len){
int i=0,j=len-1;
char tmp;
while(i<j){
tmp=str[j];
str[j]=str[i];
str[i]=tmp;
j--;i++;
}
}
char * floatToString(double f ,char * str, int fractionPartSize)
{
ofstream fout1 ("float1.out");
int sign=f<0?1:0;
int pos =0;
int intPart = (int)f;
double fractionPart= f-intPart;
//one char at a time , first int part then fraction part
if(intPart==0)
{
str[pos++]='0';
}
else if(sign==1)
{
str[pos++]='-';
intPart=-intPart;
}
while(intPart>0){
str[pos++]='0'+intPart%10;
intPart/=10;
}
int intPartSize=pos-sign;
reverse(str+sign,intPartSize);
//int i=0;
str[pos++]='.';
while(pos<fractionPartSize+intPartSize+1){
f=f-(int)f;
f*=10;
str[pos++]='0'+(int)f;
}
str[pos]='\0';
fout1<<"string"<<str;
}
int main()
{
ifstream fin ("float.in");
ofstream fout("float.out");
char mainStr[MAXLEN];
double f;
fin>>f;
int fractionPartSize=6;
floatToString(f,mainStr,fractionPartSize);
return 0;
}
#include<iostream.h>
#include<string.h>
string reverse (string src)
{
int i = 0, j = src.length() - 1;
while(i < j)
{
char temp = src.at(i);
src.at(i) = src.at(j);
src.at(j) = temp;
i++;
j--;
}
return src;
}
string intToString(int src)
{
string result;
do
{
int res = (int)'0' + src%10;
src = src/10;
result.push_back(res);
}
while(src != 0);
return reverse(result);
}
int main(int argc, char* argv[])
{
string str;
float f = 7.65;
int intPart = (int)f;
int decPart = 0;
float temp = f - int(f);
while(temp - (int)temp != 0.0)
{
temp = temp*10;
}
decPart = temp;
str = intToString(intPart)+ '.'+ intToString(decPart);
cout << str;
cin.get();
return 0;
}
float x = 7.98;
char a[5] ;
for (int i =0; i<4; i++){
int m = (int)x;
if(i==1)
a[i] = '.';
else
a[i] = m+48;
x = x-m;
x = x*10;
}
a[4] = '\0';
string str = a;
cout<<str<<endl;
int convert_f_t_s(float num, int decimal_num)
{
int i = (int) num;
char *str = NULL;
float decimal = num - i;
int i_num = 0;
int tmp = 0;
int j = 0;
while (i) {
i = i / 10;
i_num++;
}
str = (char *)malloc(i_num + decimal_num + 2);
memset (str, 0, i_num + decimal_num + 2);
i = (int) num;
decimal = num - i;
for (j = 0; j < i_num; j++) {
tmp = i % 10;
i = i / 10;
str[i_num - j -1] = '0' + tmp;
}
str[i_num] = '.';
for (j = 0; j < decimal_num; j++) {
decimal = decimal * 10;
tmp = ((int)decimal % 10);
str[i_num + j + 1] = '0' + tmp;
}
printf("orl str is %s\n", str);
}
int main(int argc, char **argv)
{
float num = 34.234;
convert_f_t_s(num, 3);
}
class Float2String
- karthik dheeraj September 06, 2013{
public static void main(String[] args)
{
float f=7.64f;
String result;
result=""+f;
System.out.println(result);
}
}