Package com.singularsys.jep.misc.functions

# Class ToBase

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryFunction
com.singularsys.jep.misc.functions.ToBase

All Implemented Interfaces:
  `PostfixMathCommandI`, `Serializable`

public class ToBase extends NaryFunction
Convert a number to a string in a given base. The base to used is specified in the constructor

A prefix can be specified in the constructor. If set this will be appended to the number (after minus sign for negative values). For example with the following setup

     jep.addFunction("toBase",new ToBase());
     jep.addFunction("toDec",new ToBase(10));
     jep.addFunction("toHex",new ToBase(16,"0x"));

- `toBase(val,12)` converts to base 12 numbers.
- `toBase(val,16,3)` converts to base 12 with 3 hex digits after decimal place.
- `toDec(val)` converts to base 10.
- `toDec(val,3)` converts to base 10 with 3 digits after decimal place.
- `toHex(val)` converts to base 16.
- `toHex(val,3)` converts to base 16 with 3 hex digits after decimal place.

A prefix can be specified in the constructor. If set this will be appended to the number (after minus sign for negative values).

Author:
  Rich Morris Created on 02-May-2005
See Also:
  - `Long.toString(long, int)`
  - Serialized Form

- ## Field Summary

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

  `curNumberOfParameters, description, name, NaN, numberOfParameters`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ToBase()`
  Constructor where base is specified as a function argument.
  `ToBase(int base)`
  Constructor with specified base.
  `ToBase(int base, String prefix)`
  Constructor with specified base and a given prefix.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `checkNumberOfParameters(int n)`
  Checks the number of parameters of the function.
  `Object`
  `eval(Object[] args)`
  Evaluate the function
  `String`
  `toBase(double val, int base, int digits)`
  Converts a number to a give base.
  `String`
  `toBase(long num, int base)`
  Converts a number to a give base.

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

  `run`

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

  - ### ToBase

    public ToBase()
    Constructor where base is specified as a function argument.

  - ### ToBase

    public ToBase(int base)
    Constructor with specified base.
    Parameters:
      `base` - the base to use
    Throws:
      `IllegalArgumentException` - if base is < 2 or > 36

  - ### ToBase

    public ToBase(int base, String prefix)
    Constructor with specified base and a given prefix. For example 0x to proceed hexadecimal numbers.
    Parameters:
      `base` - the base to use
      `prefix` - the string to prefix numbers with.
    Throws:
      `IllegalArgumentException` - if base is < 2 or > 36

- ## Method Details

  - ### checkNumberOfParameters

    public boolean checkNumberOfParameters(int n)
    Description copied from class: `PostfixMathCommand`
    Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method
    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.

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

  - ### toBase

    public String toBase(long num, int base)
    Converts a number to a give base.
    Parameters:
      `num` - number to convert
      `base` - base to use
    Returns:
      String representation
    Throws:
      `IllegalArgumentException` - if base is < 2 or > 36

  - ### toBase

    public String toBase(double val, int base, int digits)
    Converts a number to a give base.
    Parameters:
      `val` - number to convert
      `base` - base to use
      `digits` - number of digits after decimal place
    Returns:
      String representation
    Throws:
      `IllegalArgumentException` - if base is < 2 or > 36