roundto
RoundDirection
Controller: CodeCogs
Interface
C++
RoundTo
template<class A> AroundTo( | A | val | |
int | decimalPlaces | ) |
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
val the value to be rounded decimalPlaces the 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> AroundTo( | A | val | |
int | decimalPlaces | ||
RoundDirection | rd | ) |
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
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.
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> AtruncTo( | A | val | |
int | decimalPlaces | ) |
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
val the value to be truncated decimalPlaces the 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.