01: /*
02: * @(#)formatPercent.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-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.lib;
10:
11: import java.text.*;
12: import java.util.*;
13: import pnuts.lang.Context;
14: import pnuts.lang.PnutsFunction;
15:
16: public class formatPercent extends PnutsFunction {
17:
18: public formatPercent() {
19: super ("formatPercent");
20: }
21:
22: public boolean defined(int nargs) {
23: return nargs == 1 || nargs == 2 || nargs == 5;
24: }
25:
26: protected Object exec(Object[] args, Context context) {
27: int nargs = args.length;
28: if (nargs == 1) {
29: return NumberFormatHelper.formatPercent((Number) args[0],
30: -1, -1, -1, -1, context);
31: } else if (nargs == 2) {
32: Number num = (Number) args[0];
33: Object arg1 = args[1];
34: if (arg1 instanceof Integer) {
35: return NumberFormatHelper.formatPercent(num, -1, -1,
36: ((Integer) arg1).intValue(), -1, context);
37: } else {
38: throw new IllegalArgumentException();
39: }
40: } else if (nargs == 5) {
41: Number num = (Number) args[0];
42: int min = ((Number) args[1]).intValue();
43: int max = ((Number) args[2]).intValue();
44: int fmin = ((Number) args[3]).intValue();
45: int fmax = ((Number) args[4]).intValue();
46: return NumberFormatHelper.formatPercent(num, min, max,
47: fmin, fmax, context);
48: } else {
49: undefined(args, context);
50: return null;
51: }
52: }
53:
54: public String toString() {
55: return "function formatPercent(num),(num,min),(num,min,max,fmin,fmax)";
56: }
57: }
|