public class ToBase extends NaryFunction
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).
Long.toString(long, int)
,
Serialized FormcurNumberOfParameters, name, NaN, numberOfParameters
Constructor and Description |
---|
ToBase()
Constructor where base is specified as a function argument.
|
ToBase(int base)
Constructor with specified base.
|
ToBase(int base,
java.lang.String prefix)
Constructor with specified base and a given prefix.
|
Modifier and Type | Method and Description |
---|---|
boolean |
checkNumberOfParameters(int n)
Checks the number of parameters of the function.
|
java.lang.Object |
eval(java.lang.Object[] args)
Evaluate the function
|
java.lang.String |
toBase(double val,
int base,
int digits)
Converts a number to a give base.
|
java.lang.String |
toBase(long num,
int base)
Converts a number to a give base.
|
run
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, getName, getNumberOfParameters, setCurNumberOfParameters, setName, toString, toString
public ToBase()
public ToBase(int base)
base
- the base to usejava.lang.IllegalArgumentException
- if base is < 2 or > 36public ToBase(int base, java.lang.String prefix)
base
- the base to useprefix
- the string to prefix numbers with.java.lang.IllegalArgumentException
- if base is < 2 or > 36public boolean checkNumberOfParameters(int n)
PostfixMathCommand
checkNumberOfParameters
in interface PostfixMathCommandI
checkNumberOfParameters
in class PostfixMathCommand
n
- number of parameters function will be called with.public java.lang.Object eval(java.lang.Object[] args) throws EvaluationException
NaryFunction
eval
in class NaryFunction
args
- arguments to the functionEvaluationException
- if the calculation cannot be performedpublic java.lang.String toBase(long num, int base)
num
- number to convertbase
- base to usejava.lang.IllegalArgumentException
- if base is < 2 or > 36public java.lang.String toBase(double val, int base, int digits)
val
- number to convertbase
- base to usedigits
- number of digits after decimal placejava.lang.IllegalArgumentException
- if base is < 2 or > 36Copyright © 2018 Singular Systems http://www.singularsys.com/jep