You are here:

C++/Rounding to specified decimal point length

Advertisement


Question
Hello,

Want to round a double value to certain number of decimal places using Visual C++ 6.0 SP5.

I always display sixteen [16] decimal places. There does not appear to be a rounding function availble in the SDK for Visual C++ 6.0. At least I could not find it in MSDN library of October 2001, last compatible MSDN release for  Visual C++ 6.0.

Are you aware of any rounding techniques that allow control of decimal point length for said compiler ?

Do I need to create my own rounding function ?

Thank you for your time and consideration.

Answer
Hello Ray, thank you for the question.

The way to round decimal numbers the easiest is to use some kind of epislilon value, but that can become a complicated and cumbersome process.

The easy way is to set the precision to the desired number of decimal places with cout as such:

// Show the decimal places
cout.setf(ios_base::fixed, ios_base::floatfield);

// Number of decimal places to show(2, for example)
cout.precision(2);

If you happen to be using the C-style way to print to the console, it is even easier:

printf("Float: %.02f", floatNum);

Specifies 2 decimal places for that example.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.