RoundDirection

View versions (1)

Interface

Overview

  • rd_AwayZero The value is rounded away from zero.
  • rd_TowardZero The value is rounded towards zero.
GPL Licence — free for non commercial use. See Licence details.

FUNCTION

roundTo

Round to the nearest value, to a specified number of decimal places. Half-way cases are rounded away from zero, such that:

|x_{round}| > |x|
(1)

where: x is the initial value, xround is the result.

The function is equivalent to the Microsoft Excel function ROUND.

decimalPlaces specifies the number of decimal places to round to. Positive values of of decimalPlaces round to the right of the decimal point. Negative values round to the left of the decimal place.

The function works using the following method:

x_{round}\ =\ \frac{round(x*10.0^{decimalPlaces})}
                       {10.0^{decimalPlaces}}
(2)

where: round converts its parameter into an integer, rounding half-way cases away from zero.

This function may raise the inexact floating point exception for non-integer numeric arguments of val. However, this should not be relied upon.

Example 1

#include <iostream>
#include <codecogs/computing/io/format/roundto.h>
using namespace std;

int main() 
{
  double n=437.615;
   
  cout<<"rounding "<<n<<" to 0 d.p. "<<IO::Format::roundTo(n, 0)<<endl;
  cout<<"rounding "<<n<<" to 1 d.p. "<<IO::Format::roundTo(n, 1)<<endl;
  cout<<"rounding "<<n<<" to 2 d.p. "<<IO::Format::roundTo(n, 2)<<endl;
  cout<<"rounding "<<n<<" to 3 d.p. "<<IO::Format::roundTo(n, 3)<<endl;
  
  cout<<"rounding "<<n<<" to -1 d.p. "<<IO::Format::roundTo(n, -1)<<endl; 
  cout<<"rounding "<<n<<" to -2 d.p. "<<IO::Format::roundTo(n, -2)<<endl;
  
  n=-1.475;
  cout<<"rounding "<<n<<" to 2 d.p. "<<IO::Format::roundTo(n, 2)<<endl;
  return 1;
}

Output:

rounding 437.615 to 0 d.p. 438
rounding 437.615 to 1 d.p. 437.6
rounding 437.615 to 2 d.p. 437.62
rounding 437.615 to 3 d.p. 437.615
rounding 437.615 to -1 d.p. 440
rounding 437.615 to -2 d.p. 400
rounding -1.475 to 2 d.p. -1.48

Parameters

val
the value to be rounded
decimalPlaces
the number of decimal places to round to

Returns

the rounded value

Zero is an acceptable number of decimal places to round to, this will yield an integral value.

Author

James Warren (July 2005)

FUNCTION

roundTo

Round a value in the specified direction, to the specified number of decimal places. In the case of rounding away from zero, all return values are such that:

|x_{round}| >= |x|
(3)

And in the case of rounding toward zero, all return values are such that:

|x_{round}| <= |x|
(4)

where: x is the initial value, xround is the result.

The function supplies the functionality of the Microsoft Excel functions ROUNDUP and ROUNDDOWN.

decimalPlaces specifies the number of decimal places to round to. Positive values of of decimalPlaces round to the right of the decimal point. Negative values round to the left of the decimal place.

The function works using the following method:

x_{round}\ =\ \frac{round(x*10.0^{decimalPlaces})}
                       {10.0^{decimalPlaces}}
(5)

where: round converts its parameter into an integer, rounding in the specified direction.

This function may raise the inexact floating point exception for non-integer numeric arguments of val. However, this should not be relied upon.

Example 1

#include <iostream>
#include <codecogs/computing/io/format/roundto.h>
using namespace std;

int main()  
{
  cout<<"rounding 3.2 to 0 d.p. ";
  cout<<IO::Format::roundTo(3.2, 0, IO::Format::rd_AwayZero)<<endl;
  
  cout<<"rounding 76.9 to 0 d.p. ";
  cout<<IO::Format::roundTo(76.9, 0, IO::Format::rd_AwayZero)<<endl;
  
  cout<<"rounding 3.14159 to 3 d.p. ";
  cout<<IO::Format::roundTo(3.14159, 3, IO::Format::rd_AwayZero)<<endl;
  
  cout<<"rounding -3.14159 to 1 d.p. ";
  cout<<IO::Format::roundTo(-3.14159, 1, IO::Format::rd_AwayZero)<<endl;
  
  printf("rounding %f to %i d.p. %f\n", 31415.92654, -2,
         IO::Format::roundTo(31415.92654, -2, IO::Format::rd_AwayZero));
    
  cout<<"rounding 3.2 to 0 d.p. ";
  cout<<IO::Format::roundTo(3.2, 0, IO::Format::rd_TowardZero)<<endl;
  
  cout<<"rounding 76.9 to 0 d.p. ";
  cout<<IO::Format::roundTo(76.9, 0, IO::Format::rd_TowardZero)<<endl;
  
  cout<<"rounding 3.14159 to 3 d.p. ";
  cout<<IO::Format::roundTo(3.14159, 3, IO::Format::rd_TowardZero)<<endl;
  
  cout<<"rounding -3.14159 to 1 d.p. ";
  cout<<IO::Format::roundTo(-3.14159, 1, IO::Format::rd_TowardZero)<<endl;
  
  printf("rounding %f to %i d.p. %f\n", 31415.92654, -2,
         IO::Format::roundTo(31415.92654, -2, IO::Format::rd_TowardZero));
    
  return 1;
}

Output:

rounding 3.2 to 0 d.p. 4
rounding 76.9 to 0 d.p. 77
rounding 3.14159 to 3 d.p. 3.142
rounding -3.14159 to 1 d.p. -3.2
rounding 31415.926540 to -2 d.p. 31500.000000
rounding 3.2 to 0 d.p. 3
rounding 76.9 to 0 d.p. 76
rounding 3.14159 to 3 d.p. 3.141
rounding -3.14159 to 1 d.p. -3.1
rounding 31415.926540 to -2 d.p. 31400.000000

Parameters

val
the value to be rounded.
decimalPlaces
the number of decimal places to round to.
rd
the rounding direction, either rd_TowardZero or rd_AwayZero.

Returns

the rounded value.

Zero is an acceptable number of decimal places to round to, this will yield an integral value.

Author

James Warren (July 2005)

FUNCTION

truncTo

Truncate a value, to a specified number of decimal places. The function is equivalent to the Microsoft Excel function TRUNC.

decimalPlaces specifies the number of decimal places to truncate to. Positive values of of decimalPlaces truncate to the right of the decimal point. Negative values truncate to the left of the decimal place.

The function works using the following method:

x_{t}\ =\ \frac{truncate(x*10.0^{decimalPlaces})}
                   {10.0^{decimalPlaces}}
(6)

where: truncate truncates its parameter into an integer.

Output:

truncating 8.9 to 0 d.p. 8
truncating -8.9 to 0 d.p. -8
truncating 3.14159 to 0 d.p. 3
truncating 3.14159 to 3 d.p. 3.141
truncating -314.159 to -1 d.p. -310

Example 1

#include <iostream>
#include <codecogs/computing/io/format/roundto.h>
using namespace std;

int main()  
{
  cout<<"truncating 8.9 to 0 d.p. ";
  cout<<IO::Format::truncTo(8.9, 0)<<endl;
  
  cout<<"truncating -8.9 to 0 d.p. ";
  cout<<IO::Format::truncTo(-8.9, 0)<<endl;
  
  cout<<"truncating 3.14159 to 0 d.p. ";
  cout<<IO::Format::truncTo(3.14159, 0)<<endl;
  
  cout<<"truncating 3.14159 to 3 d.p. ";
  cout<<IO::Format::truncTo(3.14159, 3)<<endl;
  
  cout<<"truncating -314.159 to -1 d.p. ";
  cout<<IO::Format::truncTo(-314.159, -1)<<endl;
 
  return 1;
}

Parameters

val
the value to be truncated
decimalPlaces
the number of decimal places to truncate to

Returns

the truncated value

Zero is an acceptable number of decimal places to truncate to, this will yield an integral value.

Author

James Warren (July 2005)