Solved

decimal rounding

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

example:

I have   3.21453235
I want   3.21

Thanks
0
Comment
Question by:talaskam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
MatthewL earned 50 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 ).

Matt

#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 );
}
0
 
LVL 84

Expert Comment

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

Expert Comment

by:nietod
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 );
0
 

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

635 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