decimal rounding

Posted on 1998-12-12
Medium Priority
Last Modified: 2012-05-04
What's the easiest way to round (or truncate) a double value?


I have   3.21453235
I want   3.21

Question by:talaskam

Accepted Solution

MatthewL earned 100 total points
ID: 1180028
Hi talaskam;

You can shift the number the required number of decimal places, truncate it, and shift it back again.  The function round listed below does that.  It has a limitation on the size of the numbers used ( since I've cast into a long int ).


#include <iostream.h>
#include <math.h>

double round( double number, int decPlace );

void main( void )
      double value = 3.21453235;

      value = round( value, 2 );

      cout << value << endl;


double round( double number, int decPlace )
      long int temp;

      number *=  pow( 10, decPlace );
      temp = (long int)( number + .5 );
      number = ( double ) temp;

      number /= pow( 10, decPlace );

      return( number );
LVL 85

Expert Comment

ID: 1180029
Now try it with
value = round( -3.21453235, 2);
LVL 22

Expert Comment

ID: 1180030
To avoid the limitations of 32 bit integers and (I believe) to have the correct (well, most accepted) rounding for negatives, use

number *=  pow( 10, decPlace );
number = floor( number + .5 );
number /= pow( 10, decPlace );

Expert Comment

ID: 1180031
iff this is for printing the value you can just use the width and precision stuff for printf.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question