Cutting a number to last 2 digits.

Sep 6, 2008 at 4:35am
Ok, I have to do an operation like this:

454*22.76 = 10 333.04

Thats ez, but then I have to only print to screen the last two digits, the 33.

How would I do this, to multiply two integers then make the product a double then only print out the last two digits?

Would I have to assign something to each number and only print out n1 and n2?

Im lost lol
Last edited on Sep 6, 2008 at 4:39am
Sep 6, 2008 at 5:04am
Sep 6, 2008 at 5:43am
Can't you do that with C style print such as:

printf("The number is %2.0f \n", number);

I like the modulo, but that wouldn't help for getting rid of the decimal places would it?
Sep 6, 2008 at 5:54am
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){
	for (int a=0;a<80;a++)
		printf(".");
	printf("%2.0f\n",1024.48);
	printf("%2.0f\n",4.48);
	for (int a=0;a<80;a++)
		printf(".");
	return 0;
}

Output:

................................................................................
1024
4
................................................................................



Actually, since modulo only works on integers, the values would be converted to int and then operated upon.
Last edited on Sep 6, 2008 at 5:57am
Sep 6, 2008 at 6:00am
Yeah, I'm on my computer that doesn't have the code interface set up right now so I couldn't test it. I was almost positive there was a way to do this though, but looking at the formatters now, I'm not so sure.
Sep 8, 2008 at 6:03am
if you like the modulo:
force the long to an int by n=(int n)
then use %.
Topic archived. No new replies allowed.