# How to round off by 2 decimal places in C++?



## BradleyW

I need to round off the value held within a variable in the final output of my program.
Here is an example of what I am dealing with.

Code:



Code:


long float cost = x amount 
cout << "The cost of shipment over" << " " << miles << " " << "miles is" << " " << poundsign << cost << endl; //output result

Right now, the program will display the following: "The cost of shipment over 550.50 miles is £ (x amount)".
I need it to say the following: "The cost of shipment over 550 miles is £ (x amount)".

Thanks for the help and support.

Edit: Found a partial solution but it also rounds off the miles variable

Code:



Code:


cout.setf(ios::fixed);
 cout << setprecision (2);
 cout << "The cost of shipment over" << " " << miles << " " << "miles is" << " " << poundsign << cost << endl;

Edit: Solution found. Ignore system pause. That's just to stop the program.

Code:



Code:


cout << "The cost of shipment over" << " " << miles << " " << "miles is" << " ";
 cout.setf(ios::fixed);
 cout << setprecision(2);
 cout << poundsign << cost << endl; //output result
system("pause"); //end program


----------



## tompsonn

Alternately you can just cast the float to a long, which removes the precision. Looks like you just want truncating conversion, not rounding (?) so this would work simpler.


----------



## FlyingNugget

Hey Brad that will work for rounding in the ostream. However if you wanted to round a value held by a variable... 100.00 is for two decimal places. 1000.00 for 3, etc.

Code:



Code:


double num = 1.12745;
num = floor( num * 100.00 + 0.5 ) / 100.00;
std::cout << num << std::endl;


----------



## BradleyW

Quote:


> Originally Posted by *tompsonn*
> 
> Alternately you can just cast the float to a long, which removes the precision. Looks like you just want truncating conversion, not rounding (?) so this would work simpler.


Quote:


> Originally Posted by *FlyingNugget*
> 
> Hey Brad that will work for rounding in the ostream. However if you wanted to round a value held by a variable... 100.00 is for two decimal places. 1000.00 for 3, etc.
> 
> Code:
> 
> 
> 
> Code:
> 
> 
> double num = 1.12745;
> num = floor( num * 100.00 + 0.5 ) / 100.00;
> std::cout << num << std::endl;


Thanks for this.
I am interested in how you cast a float to a long? I've not heard of this before?
I am in need of getting the following output:

Can you see that the numbers after the decimal point have gone whilst not rounding the number?
How may I do this?
Thank you.


----------



## tompsonn

Quote:


> Originally Posted by *BradleyW*
> 
> Thanks for this.
> I am interested in how you cast a float to a long? I've not heard of this before?
> I am in need of getting the following output:
> 
> Can you see that the numbers after the decimal point have gone whilst not rounding the number?
> How may I do this?
> Thank you.


Yes this is called truncating conversion. Simply casting a floating point type to an integral type removes the precision - it does not do any rounding, which is what you want. Take this code example:

Code:



Code:


float cost = 550.50
long cost_int =  ( long ) cost; /* the ( long ) casts the float to a long */

cout << cost_int;

Alternately:

Code:



Code:


float cost = 550.50;
cout << ( long ) cost;


----------



## poroboszcz

Are you sure you're storing the input value in the right type of variable? There is no such type as "long float". Use float or double. Other than that your solution above should work.

Code:



Code:


double miles;
std::cin >> miles;
std::cout << std::fixed << std::setprecision(2) << miles << std::endl;


----------



## tompsonn

Quote:


> Originally Posted by *poroboszcz*
> 
> Are you sure you're storing the input value in the right type of variable? There is no such type as "long float". Use float or double. Other than that your solution above should work.
> 
> Code:
> 
> 
> 
> Code:
> 
> 
> double miles;
> std::cin >> miles;
> std::cout << std::fixed << std::setprecision(2) << miles << std::endl;


Indeed there is no "long float" that I know of. There is however, a "long double" (MSVC at least), however its the same width, and the same as, simply a "double".

Not sure what compiler he's using... or there could be a typedef or macro lurking about there...


----------



## BradleyW

Thank you very much everyone. This collection of code is very useful to study. As for the long float, I assumed it is just a float with more bits reserved for longer/larger numbers. I am using Visual Studio 2012 Professional


----------



## tompsonn

Quote:


> Originally Posted by *BradleyW*
> 
> Thank you very much everyone. This could is very useful to study. As for the long float, I assumed it is just a float with more bits reserved for longer/larger numbers. I am using Visual Studio 2012 Professional


A float is 4 bytes wide (32-bit) - a double is 8 bytes wide (64-bit). Perhaps the compiler allows you to specify "long float" however this type certainly is NOT in the table on MSDN for C++ fundamental types. If you're after a larger width floating point type, use "double".


----------



## BradleyW

Quote:


> Originally Posted by *tompsonn*
> 
> A float is 4 bits wide - a double is 8 bits wide. Perhaps the compiler allows you to specify "long float" however this type certainly is NOT in the table on MSDN for C++ fundamental types. If you're after a larger width floating point type, use "double".


Thank you for the information. I tend to use double as a standard, or Integer if I am only dealing with small whole numbers only.


----------



## deepor

tompsonn mistyped and wanted to say it's 4 and 8 "bytes" not bits.

I only know for sure how it was in C from a long time ago:

float was a 32-bit wide floating point number
double was a 64-bit wide floating point number
long double was what used the full 80-bit of the x87 coprocessor

Those 80-bit numbers are strange. I don't know what happens today with long double because of that. Compilers should try to not touch x87 code in any case as it's weird and slow compared to SSE2 and AVX and stuff. But I don't think you can do 80-bit numbers without x87. SSE2 should only do 64-bit. So you should probably never touch long double.

It being 80-bit wide may seem weird and off today, but x87 started out in 16-bit processor times. Those 80 bits are just one 16-bit word added to 64 bits.


----------



## tompsonn

Quote:


> Originally Posted by *deepor*
> 
> tompsonn mistyped and wanted to say it's 4 and 8 "bytes" not bits.
> 
> I only know for sure how it was in C from a long time ago:
> 
> float was a 32-bit wide floating point number
> double was a 64-bit wide floating point number
> long double was what used the full 80-bit of the x87 coprocessor
> 
> Those 80-bit numbers are strange. I don't know what happens today with long double because of that. Compilers should try to not touch x87 code in any case as it's weird and slow compared to SSE2 and AVX and stuff. But I don't think you can do 80-bit numbers without x87. SSE2 should only do 64-bit. So you should probably never touch long double.
> 
> It being 80-bit wide may seem weird and off today, but x87 started out in 16-bit processor times. Those 80 bits are just one 16-bit word added to 64 bits.


You bet I mistyped! Thanks









On MSVC at least now, a "long double" is the same as a "double" - however they *are* two different types, so you must use either or.


----------

