Solved

C++ round to n places

Posted on 2006-11-20
9
1,406 Views
Last Modified: 2008-01-09
Hi experts,
I have two related questions:

(A)
I would like to know if there is a fuction to round double values to n decimal places e.g.

double value = 0.000000000234567897

value = round(value, 12) // now value is 0.000000000235 i.e to 12 decimal places

(B)
How do I watch a double value to n decimal places in visual studio c++. When I hover over a double variable I can only see up to 6 or 7 decimal places?

Thank you,
j

0
Comment
Question by:XPUSR
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 17982189

you could multiply the number by 10^places, truncate it to an integer, then divide by 10^places.

0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 17982370
or you could include the following:

#include <iostream>
#include <iomanip>
using namespace std;

and then use manipulator setprecision(int t);

which will round your number at first t-1 decimal positions.

for example:

double x = 1.23456789;
cout << setprecision(4) << x;

will print 1.234.

Howerever, when numbers are small like your double value, then compiler will
use exponential notation for example 2.346 e-010 (in this case), where
"first number" is rounded at t-1 decimal places.

so:

double value = 0.00000000023456789;
cout << setprecision(4) << value;

will print 2.346 e-010.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 17982424
Sorry about a little mistake, in those t positions are counted digits both after and before point.
You can see complete explanation here.

http://www.cplusplus.com/ref/iostream/iomanip/setprecision.html
0
 

Author Comment

by:XPUSR
ID: 17982769
Thanks very much but I cannot use cout as I am in a COM environment. Is there another way?


Also,

double TeamAScore1 = 7.00;
double TeamAScore2 = 7.02;
double performace = (TeamAScore2 /TeamAScore1) - 1;

If I debug the code above I get a value of "0.0028571428571429" in the watch window for the performance variable, however if I do the following:

double endperformace = performace - 0.0028571428571429;

the endperformace variable is not 0 but "-8.6736173798840e-018". Can anyone explain this?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:XPUSR
ID: 17983033
Is there a mehod of outputing data to n decimal places to the visual c++ output window?
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17984416
Hello, give this a shot:

                         double value = 0.000000000234567897;
                         double rounded;
                         int n = 12;
                         rounded = System::Math::Round(value, n);
                         MessageBox::Show("value: "+value+"\nrounded: "+rounded);

For Round() use the 4th function of it (4 of 8). The default will round to the nearest integer. The 4th rounds to desired precision.
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17984420
0
 

Author Comment

by:XPUSR
ID: 17985340
>> YoungBonzi
I think the System::Math::Round is a .NET method. I can't seem to get it to work for non .Net apps?
0
 
LVL 4

Accepted Solution

by:
bdunz19 earned 130 total points
ID: 18001371
For the rounding:

float RoundUp(float Value, int Places)
{
      float tmp = 0.0;
      int hold = 0;
      hold = (int)(Value * (float)pow(10, Places) + 0.5);
      tmp = (float)hold / (float)pow(10, Places);

      return tmp;
}

As for long numbers, the best I can come up with is to store your entire number in an advanced double array. Meaning each increment in the array stores the value of data to a further percision (or maybe some simple short array with one single digit value for each percision).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 170
IdTCPClient1->Disconnect(); not working 3 59
How to convert MFC APP to Win32 APP. 19 68
Why isn't object file created? 6 39
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now