please :)

HI
how can i convert fraction to integer
example:
the number is 0.45
i want make it 45
how can i do it ?
thanks :)
Times 100?
No
i don't know the number
i will take it from user :)
Have you done any code yourself? If so you should post it that way people will know what you're actually wanting.
#include<iostream>
using namespace std;
int main()
{
double n;
cin>>n; // say n = 0.34
cout<<n; // i want cout<< 34
}
Well you're already there and what takzee said is still what you need to do. Your variable n holds the value of whatever the user enters. So just multiply n by 100 and that will give you your answer.
say n=.0034 i want n= 34
or n=.8903 i want n= 8903


you understand me
The fact is that what you think is not there in a double

For eg. 1/3 = 0.33333333.... but in a language with base 3, it is 0.1
Similarly, as a computer can only store data like 2s, 0.1 would be 0.000(1100) recurring in base 2.
As a computer, can't such data, it stores an approximate value.

Hence, if you think a double stores 2.345, it might actually be storing 2.3450000000000002.

But there is a way which will only work for small no. of decimal places

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
void main()
{
	double d;
	cout<<"Enter a no. ";
	cin>>d;
	while(float(d)!=float(double(int(d))))d*=10;
	cout<<d<<endl;
}


The logic is that you keep multiplying with 10 until the number's decimal part is 0, i.e., integral part==number
Last edited on
Very good response, I'm a bigger fan of modulus, but it doesn't work with doubles which I just recently learned. I may need to use that next time, thanks for that sharma.
Topic archived. No new replies allowed.