Package com.singularsys.jep.functions

# Class Round

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryFunction
com.singularsys.jep.functions.Round

All Implemented Interfaces:
  `PostfixMathCommandI`, `Serializable`

Direct Known Subclasses:
  `BigDecRound`, `RInt`, `RoundBD`

public class Round extends NaryFunction
A PostfixMathCommandI which rounds a number. The single argument version, `round(a)`, uses `Math.round(double)` to round to the closest integer. The two argument version `round(a,3)` rounds the argument to 3 decimal places.

Note: This class was changed in version 3.4. Prior to 3.4, this class used the Math.rint method. In 3.4 and future releases Math.round will be used instead. The effect is changing the rounding behaviour when the input is equally close to two integers, Math.round rounds 1.5 down and Math.rint round it up.

In Jep 4.1 fixes a bug with `round(100000,-5) -> 99999.99999999999`. An alternative algorithm is provided by the `RoundBD` class, this uses BigDecimal's `setscale(scale,mode)` method. This can improve results for cases like `round(265.335,2)` where the floating-point representation of 265.335 is not exact.

Author:
  Richard Morris
See Also:
  - `RInt`
  - `RoundBD`
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `curNumberOfParameters, description, name, NaN, numberOfParameters`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Round()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `checkNumberOfParameters(int n)`
  Must have one or two parameter
  `Object`
  `eval(Object[] args)`
  Evaluate the function
  `protected double`
  `round(double val)`
  The rounding method which implements the core rounding operation used in this class.
  `Object`
  `round(Object param)`
  `Object`
  `round(Object l, Object r)`
  `void`
  `run(Stack<Object> inStack)`
  Creates an array of object then calls `NaryFunction.eval(Object[])`

  ### Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, setName, toString, toString`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### Round

    public Round()

- ## Method Details

  - ### checkNumberOfParameters

    public boolean checkNumberOfParameters(int n)
    Must have one or two parameter
    Specified by:
      `checkNumberOfParameters` in interface `PostfixMathCommandI`
    Overrides:
      `checkNumberOfParameters` in class `PostfixMathCommand`
    Parameters:
      `n` - number of parameters function will be called with.
    Returns:
      false if an illegal number of parameters is supplied, true otherwise.

  - ### run

    public void run(Stack<Object> inStack) throws EvaluationException
    Description copied from class: `NaryFunction`
    Creates an array of object then calls `NaryFunction.eval(Object[])`
    Specified by:
      `run` in interface `PostfixMathCommandI`
    Overrides:
      `run` in class `NaryFunction`
    Parameters:
      `inStack` - arguments for function
    Throws:
      `EvaluationException` - if function cannot be evaluated

  - ### round

    public Object round(Object l, Object r) throws EvaluationException
    Throws:
      `EvaluationException`

  - ### round

    public Object round(Object param) throws EvaluationException
    Throws:
      `EvaluationException`

  - ### round

    protected double round(double val)
    The rounding method which implements the core rounding operation used in this class. Other subclasses (such as RInt) can override this to perform custom rounding.
    Parameters:
      `val` - number to round
    Returns:
      the rounded value
    Since:
      3.4.0

  - ### eval

    public Object eval(Object[] args) throws EvaluationException
    Description copied from class: `NaryFunction`
    Evaluate the function
    Specified by:
      `eval` in class `NaryFunction`
    Parameters:
      `args` - arguments to the function
    Returns:
      value returned by the function
    Throws:
      `EvaluationException` - if the calculation cannot be performed