Board index » cppbuilder » ROUND(float,int); howto

ROUND(float,int); howto

Hello World!

I'm trying to round a float to one decimal and found a
dedicated function but the compiler can't find it?

Not in <math.h> anyway. It looks like a makro to me...

Is it the matissa that is getting shorter by the way?

C++Builder3 that is.

Greetings from Sweden:

    |  Sven-Olov Larsson
    |  s...@debugdata.se
    |  http://www.debugdata.se/

Dont take shit from a bug.
We try not to :)

 

Re:ROUND(float,int); howto


Tru the following:

double fround(double n, int d)
{
      return floor(n * pow(10., d) + .5) / pow(10., d);

Quote
}

Mark
Quote
Sven-Olov Larsson wrote:
> Hello World!

> I'm trying to round a float to one decimal and found a
> dedicated function but the compiler can't find it?

> Not in <math.h> anyway. It looks like a makro to me...

> Is it the matissa that is getting shorter by the way?

> C++Builder3 that is.

> Greetings from Sweden:

>     |  Sven-Olov Larsson
>     |  s...@debugdata.se
>     |  http://www.debugdata.se/

> Dont take shit from a bug.
> We try not to :)

Re:ROUND(float,int); howto


Quote
> I'm trying to round a float to one decimal and found a
> dedicated function but the compiler can't find it?

Any function that is all in uppercase is not a general BCB function. All of
these are related to the ActiveX components that ship with BCB - check the
ActiveX tab of your BCB. Unfortunately, there is no way to tell this from the
help file page, as the help files for these components are added to the entire
help system, but there is no context involved. If you keep getting confused, you
can remove them from the help by using the Help|Customize command and removing
all references to the Formula One and other OCX help files.

Here's an excellent looking template suggestion:

/*round.h
Template function takes a value and a target to round to (defaults to 1).
e.g. Round(525.345, .03) =525.36 ...Round(525.345) =525
D. Schemenauer 2000.*/
// -------------------------------------------------------------------------
--
#ifndef roundH
#define roundH
// -------------------------------------------------------------------------
--
template <class T> T Round(T value, double to_nearest =1)
{
  if(to_nearest <=0)// change the commented line to define handling of this Error.
     to_nearest =1;
     // return 0;

  int v2;

  if(value>0)
  {
     v2 =(value + to_nearest/2)/to_nearest;
     return v2 * to_nearest;
  }
  else if(value<0)
  {
     v2 =(value - to_nearest/2)/to_nearest;
     return v2 * to_nearest;
  }
  else
     return 0;

Quote
}

// ----------------------------------------------------------
#endif

+=====================================================+
| Jonathan Arnold (mailto:jdarn...@buddydog.org)      |
|         Comprehensive C++Builder link site:         |
| http://www.buddydog.org/C++Builder/c++builder.html  |
+=====================================================+

Re:ROUND(float,int); howto


Hi Jonathan!

I don't understand this template stuff?

Does it return int's? (sometimes anyway)

Is there any howtos on the net?

On Tue, 23 Jan 2001 10:20:08 -0500, Jonathan Arnold

Quote
<jdarn...@buddydog.org> wrote:

>Here's an excellent looking template suggestion:

>/*round.h
>Template function takes a value and a target to round to (defaults to 1).
>e.g. Round(525.345, .03) =525.36 ...Round(525.345) =525
>D. Schemenauer 2000.*/
>// -------------------------------------------------------------------------
>--
>#ifndef roundH
>#define roundH
>// -------------------------------------------------------------------------
>--
>template <class T> T Round(T value, double to_nearest =1)
>{
>  if(to_nearest <=0)// change the commented line to define handling of this Error.

Greetings from Sweden:

    |  Sven-Olov Larsson
    |  s...@debugdata.se
    |  http://www.debugdata.se/

Dont take shit from a bug.
We try not to :)

Re:ROUND(float,int); howto


Quote
> I don't understand this template stuff?
> Does it return int's? (sometimes anyway)

Yes, it sure does. and the nice thing about this template is you can use
it with doubles, or floats or whatever. So:

#include "round.h"

....

float x = 1.22;
int rounded_x = Round(x);

will get you rounded_x == 1.

+=====================================================+
| Jonathan Arnold (mailto:jdarn...@buddydog.org)      |
|         Comprehensive C++Builder link site:         |
| http://www.buddydog.org/C++Builder/c++builder.html  |
+=====================================================+

Other Threads