I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

Valid RSS

get GPL
COST (GBP)
this unit 0.30
sub units 0.00
+
0
ComputingIoFormat

roundto

viewed 11534 times and licensed 125 times
RoundDirection
Controller: CodeCogs

get GPL add to cart

Interface

C++

RoundTo

 
template<class A> AroundToAval
intdecimalPlaces )
Round to the nearest value, to a specified number of decimal places. Half-way cases are rounded away from zero, such that:

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:

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

Note

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

Parameters

valthe value to be rounded
decimalPlacesthe number of decimal places to round to

Returns

the rounded value

Authors

James Warren (July 2005)
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.


RoundTo

 
template<class A> AroundToAval
intdecimalPlaces
RoundDirectionrd )
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:

And in the case of rounding toward zero, all return values are such that: 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: 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 2

#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

Note

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

Parameters

valthe value to be rounded.
decimalPlacesthe number of decimal places to round to.
rdthe rounding direction, either rd_TowardZero or rd_AwayZero.

Returns

the rounded value.

Authors

James Warren (July 2005)
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.


TruncTo

 
template<class A> AtruncToAval
intdecimalPlaces )
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: 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 3

#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;
}

Note

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

Parameters

valthe value to be truncated
decimalPlacesthe number of decimal places to truncate to

Returns

the truncated value

Authors

James Warren (July 2005)
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.


Other Documentation

  • rd_AwayZero The value is rounded away from zero.
  • rd_TowardZero The value is rounded towards zero.
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.