01: /*-------------------------------------------------------------------------
02: *
03: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
04: *
05: * IDENTIFICATION
06: * $PostgreSQL: pgjdbc/org/postgresql/util/PGmoney.java,v 1.12 2007/07/16 15:02:53 jurka Exp $
07: *
08: *-------------------------------------------------------------------------
09: */
10: package org.postgresql.util;
11:
12: import java.io.Serializable;
13: import java.sql.SQLException;
14:
15: /**
16: * This implements a class that handles the PostgreSQL money and cash types
17: */
18: public class PGmoney extends PGobject implements Serializable,
19: Cloneable {
20: /*
21: * The value of the field
22: */
23: public double val;
24:
25: /*
26: * @param value of field
27: */
28: public PGmoney(double value) {
29: this ();
30: val = value;
31: }
32:
33: public PGmoney(String value) throws SQLException {
34: this ();
35: setValue(value);
36: }
37:
38: /*
39: * Required by the driver
40: */
41: public PGmoney() {
42: setType("money");
43: }
44:
45: public void setValue(String s) throws SQLException {
46: try {
47: String s1;
48: boolean negative;
49:
50: negative = (s.charAt(0) == '(');
51:
52: // Remove any () (for negative) & currency symbol
53: s1 = PGtokenizer.removePara(s).substring(1);
54:
55: // Strip out any , in currency
56: int pos = s1.indexOf(',');
57: while (pos != -1) {
58: s1 = s1.substring(0, pos) + s1.substring(pos + 1);
59: pos = s1.indexOf(',');
60: }
61:
62: val = Double.valueOf(s1).doubleValue();
63: val = negative ? -val : val;
64:
65: } catch (NumberFormatException e) {
66: throw new PSQLException(GT
67: .tr("Conversion of money failed."),
68: PSQLState.NUMERIC_CONSTANT_OUT_OF_RANGE, e);
69: }
70: }
71:
72: public boolean equals(Object obj) {
73: if (obj instanceof PGmoney) {
74: PGmoney p = (PGmoney) obj;
75: return val == p.val;
76: }
77: return false;
78: }
79:
80: public String getValue() {
81: if (val < 0) {
82: return "-$" + (-val);
83: } else {
84: return "$" + val;
85: }
86: }
87: }
|