01: /*
02: * @(#)formatNumber.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 formatNumber extends PnutsFunction {
17:
18: public formatNumber() {
19: super ("formatNumber");
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.formatNumber((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 String) {
35: return NumberFormatHelper.formatDecimal(num,
36: (String) arg1);
37: } else if (arg1 instanceof Integer) {
38: return NumberFormatHelper.formatNumber(num, -1, -1,
39: ((Integer) arg1).intValue(), -1, context);
40: } else {
41: throw new IllegalArgumentException();
42: }
43: } else if (nargs == 5) {
44: Number num = (Number) args[0];
45: int min = ((Number) args[1]).intValue();
46: int max = ((Number) args[2]).intValue();
47: int fmin = ((Number) args[3]).intValue();
48: int fmax = ((Number) args[4]).intValue();
49: return NumberFormatHelper.formatNumber(num, min, max, fmin,
50: fmax, context);
51: } else {
52: undefined(args, context);
53: return null;
54: }
55: }
56:
57: public String toString() {
58: return "function formatNumber(num),(num,{format|min}),(num,min,max,fmin,fmax)";
59: }
60: }
|