Rounding question

How would one go about getting C++ to round a number such as 15511210043330986055303168 to something like 15511210043330986000000000?

I keep getting questions on homeworks where the expected outputs are some number that ends in 00000000000 and I always seem to come up with something that ends with a bunch of numbers and no zeros at the end.

Thanks!!
I'm not sure exactly how your program is set up but you could be using a data type that is insufficient for the large numbers you are processing.

This is a basic description of how large a number each data type can handle.

http://www.cplusplus.com/doc/tutorial/variables/


For example an int variable can deal with numbers from -2147483648 to 2147483647. Thats 10 decimal places and you seem to be doing 26 decimal places.

You could use a double to deal with numbers in the range of 15 decimal places.

Beyond that you would have to figure out another solution.
I have tried double, long double, long long and everything else to no avail. :(
I don't think you would be able to use a standard data type for numbers that large. Lets assume you wanted to do an addition operation.

I'm thinking what you would need is to set up three arrays.

Have two arrays for each of the number and a third array for the result.

Maybe have it look something like

Array 1:
[1,5,5,1,1,2,1,0,0,4,3,3,3,0,9,8,6,0,5,5,3,0,3,1,6,8]

Array 2:
[1,5,5,1,1,2,1,0,0,4,3,3,3,0,9,8,6,0,0,0,0,0,0,0,0,0]

Array 3 = Array 1 + Array 2:

Set up a loop with N decimal places iterations, then add the arrays

Array3[N] = Array1[N] + Array2[N]

Then have a loop that goes through Array3's elements and if any are greater than 10 perform another operation. Subtract 10 from that element and add 1 to the element in the higher decimal place bracket.

I think regardless of the type of operation you want to do you would have to use an array of some sort. What exactly are you trying to do with numbers these large anyways? Answering that question might help guide you to a solution.
Last edited on
You may find some useful ideas in this thread:
http://www.cplusplus.com/forum/beginner/115365/
Thanks! That link is useful!!
Topic archived. No new replies allowed.