summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/cash.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2011-04-05 09:35:43 -0400
committerRobert Haas <rhaas@postgresql.org>2011-04-05 09:35:43 -0400
commitf5e524d92be609c709825be8995bf77f10880c3b (patch)
tree6b3a1754ad3c06f1146a632a77fcc56c7d23e1d1 /src/backend/utils/adt/cash.c
parent88f32b7ca29982f286b61a9d0dd29be4b8a01c25 (diff)
Add casts from int4 and int8 to numeric.
Joey Adams, per gripe from Ramanujam. Review by myself and Tom Lane.
Diffstat (limited to 'src/backend/utils/adt/cash.c')
-rw-r--r--src/backend/utils/adt/cash.c62
1 files changed, 61 insertions, 1 deletions
diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c
index 67f51280c38..4c3279759d5 100644
--- a/src/backend/utils/adt/cash.c
+++ b/src/backend/utils/adt/cash.c
@@ -26,6 +26,7 @@
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/cash.h"
+#include "utils/int8.h"
#include "utils/numeric.h"
#include "utils/pg_locale.h"
@@ -92,7 +93,6 @@ num_word(Cash value)
return buf;
} /* num_word() */
-
/* cash_in()
* Convert a string to a cash data type.
* Format is [$]###[,]###[.##]
@@ -938,3 +938,63 @@ numeric_cash(PG_FUNCTION_ARGS)
PG_RETURN_CASH(result);
}
+
+/* int4_cash()
+ * Convert int4 (int) to cash
+ */
+Datum
+int4_cash(PG_FUNCTION_ARGS)
+{
+ int32 amount = PG_GETARG_INT32(0);
+ Cash result;
+ int fpoint;
+ int64 scale;
+ int i;
+ struct lconv *lconvert = PGLC_localeconv();
+
+ /* see comments about frac_digits in cash_in() */
+ fpoint = lconvert->frac_digits;
+ if (fpoint < 0 || fpoint > 10)
+ fpoint = 2;
+
+ /* compute required scale factor */
+ scale = 1;
+ for (i = 0; i < fpoint; i++)
+ scale *= 10;
+
+ /* compute amount * scale, checking for overflow */
+ result = DatumGetInt64(DirectFunctionCall2(int8mul, Int64GetDatum(amount),
+ Int64GetDatum(scale)));
+
+ PG_RETURN_CASH(result);
+}
+
+/* int8_cash()
+ * Convert int8 (bigint) to cash
+ */
+Datum
+int8_cash(PG_FUNCTION_ARGS)
+{
+ int64 amount = PG_GETARG_INT64(0);
+ Cash result;
+ int fpoint;
+ int64 scale;
+ int i;
+ struct lconv *lconvert = PGLC_localeconv();
+
+ /* see comments about frac_digits in cash_in() */
+ fpoint = lconvert->frac_digits;
+ if (fpoint < 0 || fpoint > 10)
+ fpoint = 2;
+
+ /* compute required scale factor */
+ scale = 1;
+ for (i = 0; i < fpoint; i++)
+ scale *= 10;
+
+ /* compute amount * scale, checking for overflow */
+ result = DatumGetInt64(DirectFunctionCall2(int8mul, Int64GetDatum(amount),
+ Int64GetDatum(scale)));
+
+ PG_RETURN_CASH(result);
+}