|
I'm not sure - I also cast to int to get that result. You can use floor & ceil, which force rounding up or down, but I look forward to someone else filling in my ignorance on a function that actually rounds properly ( in the meantime, casting to int works fine ;0)
As to a number of decimal places, simply multiply by 10 ^ x, round off and divide again, or use fixed point in the first place ( where a certain number of figures in an int are regarded to be past the decimal point and calculated accordingly )
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
<br />
double Round(double dVal)<br />
{<br />
double dRemainder = fabs(fmod(dVal, 1) * (int)10);<br />
<br />
if (dVal > 0)<br />
{<br />
if (dRemainder >= (int)5)<br />
return ceil(dVal);<br />
<br />
return floor(dVal);<br />
}<br />
<br />
if (dRemainder >= (int)5)
return floor(dVal);<br />
<br />
return ceil(dVal);<br />
}<br />
<br />
<br />
double Round(double dVal, UINT nDecPlaces)<br />
{<br />
double dFactor = pow(10.0, (double)nDecPlaces);<br />
return ( Round( dVal * dFactor) / dFactor);<br />
}<br />
Andy Metcalfe - Sonardyne International Ltd (andy.metcalfe@lineone.net) http://www.resorg.co.uk
"I used to be a medieval re-enactor, but I'm (nearly) alright now..."
|
|
|
|
|
Here's some code for you. The first finction returns an int, and the second one returns a double rounded to the nearst number of decimal places. The last two functions are just in case you want a double rounded up or down to the nearest integer value. Merely adding or subtracting 0.5 to the original value and calling Rounder with that value will yield the desired result.
int Rounder(double dValue)
{
char sBuf[64];
sprintf(sBuf, "%0.lf", dValue);
return atoi(sBuf);
}
double Rounder(double dValue, int nDigits)
{
char sBuf[64];
sprintf(sBuf, "%0.*lf", nDigits, dValue);
return atof(sBuf);
}
int RoundUp(double dValue)
{
return Rounder(dValue + 0.5);
}
int RoundDown(dowble dValue)
{
return Rounder(dValue - 0.5);
}
|
|
|
|
|
Thanks a lot for this piece of useful code...
Leo.
|
|
|
|
|
Does this function really exist in <math.h> ?
Or how can set the number of decimals in my results ?
Thank in advance.
Leo.
|
|
|
|
|
I write a self_defined class "CBitmapBox" to display image.Then create a CArray object to
dynamically change CBitmapBox Object number in a dialog.But always error,error is "class CBitmapBox__thiscall CArray<class cbitmapbox,class="" cbitmapbox="">::GetAt(int) const "compile
fails.I cannot get rid of it,Please help me,Thanks.
Main code as follows:
heahfile:
CArray<cbitmapbox,cbitmapbox> *m_BmpArray;
cpp:
m_BmpArray->SetSize(TotalNum);
...
(m_BmpArray->GetAt(Num)).BLoadImgFile();//BLoadImgFile() is self_defined function.
|
|
|
|
|
GetAt() returns a copy of the object stored at index. Have you defined a copy constructor for CBitmapBox? Maybe you should use CArray::GetData() instead?
|
|
|
|
|
Instead of
CArray < CBitmapBox, CBitmapBox >
declare your array as
CArray < CBitmapBox, CBitmapBox & >
The second template parameter is important here. GetAt will return a reference to the array element. Your current GetAt returns by value, which is less efficient and requires copy constructor.
Tomasz Sowinski -- http://www.shooltz.com.pl
|
|
|
|
|
i try to use 2 table in 1 a software..I can view but i can not update both table...Recordset is read only error pops up..... After some readings i found out that i must use 2 different class for the both table...But i do know how.....????
|
|
|
|
|
Are you using MFC? The class wizard might help - for each table you want to work with, open class wizard (ctrl+w), choose Add Class (new) and select either CRecordset or CDaoRecordset as the base. It will then prompt you for tables etc. This way you can make a separate class for each table in the database - they should default to dynasets so you can update them.
You don't say what your goal is in working with the tables, so its hard for me to say if the MFC classes are what you need, but they are usually good for simple data updates etc.
|
|
|
|
|
thanx very much for the immediate reply......
I think I got it...
Thanxs to you.....
avisavc
|
|
|
|
|
Oooops.....Actully i wanna to read and write two recordset from two different table in the main window...The software is using ODBC.After i create class for each window wat do i do next...
help me.....
|
|
|
|
|
Ah - well, I'd say maybe you want to get the two recordsets working first, before designing the form/dialog that will go with them. I wouldn't go with a CRecordView in this case.
Once you get the hang of moving around the recordsets, you should be able to set up a dialog to do what you want with both recordsets represented, and you may end up with better control over things, even if at first you seem to be losing some built in functionality in the relation of the CRecordView to the CRecordset.
Always think carefully about what you want though - if there doesn't seem to be a class that represents your needs, examine your needs - especially if you are new to database design.
And thanks for the thanks
|
|
|
|
|
I coded a software.. I am having problem in the print preview... The allignment runs when i set to different printer... When i use HP1200 the alignment is correct.. When i use BJC265SP the alligment runs.. Half of the document in the print preview missing from the page.
Thanx
|
|
|
|
|
Are you printing by getting the size of the printer DC and factoring everying to that ? Otherwise it sounds like you are printing to the right DPI for the HP, but the BJC has twice the DPI.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
So how to overcome that... ???Plz help me...
|
|
|
|
|
In OnPrint get the resolution by calling
long PrintAreaHorz = GetDeviceCaps(pDC->m_hDC, HORZRES);
long PrintAreaVert = GetDeviceCaps(pDC->m_hDC, VERTRES);
then use these values to scale your page.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
Could anyone tell me what job the follow project files do? thanx
.dsw
.dsp
.opt
.ncb
|
|
|
|
|
.dsp: Project file, similar to a makefile.
.dsw: Workspace - collection of projects.
.opt: Debugging options.
.ncb: IntelliSense data. This often gets corrupted, and can safely be deleted since the IDE will automagically rebuild it.
--Mike--
http://home.inreach.com/mdunn/
#include "buffy_sig"
|
|
|
|
|
I have custom the print mechanism as the MFC function OnFilePrint.
But when I print the several copies of my document(one page only).
there always are additional pages.
my code like this:
startdoc(docinfo);
for (int i = 0; i < pInfo->m_pPD->GetCopies(); i++) {
startpage();
//print my document;
endpage();
}
enddoc();
please help me
|
|
|
|
|
Hello,
I'm new in c++ and I would like to write a program that read a JPG file and display the image in a window, but I don't know how to do the programming. Please help me.
Thank you.
Thiago Jackiw
|
|
|
|
|
You can use GDI+ to load a bitmap, or use a library like paintlib ( www.paintlib.de ). I'd recommend GDI+, just search for my surname for articles by author and you'll find all the info you need here.
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
You are so right,the screwy code would help
greatestCommonDenominator()
{
int gcd, same, hold1, hold2;
if(iNumerator < iDenominator){
for(gcd=1;gcd
|
|
|
|
|
greatestCommonDenominator()
{
int gcd, same, hold1, hold2;
if(iNumerator < iDenominator){
for(gcd=1;gcd hold1 = iDenominator / gcd;
^^^^^^^^^^ what's up with this ? Is a bit missing in the middle ? This won't compile because the for loop has no closing bracket. Does the for loop have a ; at the end of it ? If so, it won't work either.
Also, if you want the for loop to take into account more than one line, they need to also be enclosed in {}.
hold2 = iNumerator / gcd;
if(hold1 == hold2)
same = hold1;
}
Here is a gcd function
int gcd(int m, int n)
{
if (m==0) return n;
else return gcd( n%m, m);
}
Christian
#include "std_disclaimer.h"
People who love sausage and respect the law should never watch either one being made.
The things that come to those who wait are usually the things left by those who got there first.
|
|
|
|
|
Your code sample looks garbled, please put it between <pre> and </pre> tags and use < for the < sign and > for >
|
|
|
|