01: /*
02: * @(#)round.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.math;
10:
11: import pnuts.lang.PnutsFunction;
12: import pnuts.lang.Context;
13:
14: public class round extends PnutsFunction {
15:
16: public round() {
17: super ("round");
18: }
19:
20: public boolean defined(int nargs) {
21: return nargs == 1;
22: }
23:
24: protected Object exec(Object[] args, Context context) {
25: if (args.length != 1) {
26: undefined(args, context);
27: return null;
28: }
29: Object arg = args[0];
30: if (arg instanceof Float) {
31: return new Integer(Math.round(((Float) arg).floatValue()));
32: } else {
33: return new Long(Math.round(((Number) arg).doubleValue()));
34: }
35: }
36:
37: public String toString() {
38: return "function round(number)";
39: }
40: }
|