3 * Written by D'Arcy J.M. Cain
5 * http://www.druid.net/darcy/
7 * Functions to allow input and output of money normally but store
8 * and handle it as 64 bit ints
10 * A slightly modified version of this file and a discussion of the
11 * workings can be found in the book "Software Solutions in C" by
12 * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7 except that
13 * this version handles 64 bit numbers and so can hold values up to
14 * $92,233,720,368,547,758.07.
16 * src/backend/utils/adt/cash.c
25 #include "common/int.h"
26 #include "libpq/pqformat.h"
27 #include "utils/builtins.h"
28 #include "utils/cash.h"
29 #include "utils/int8.h"
30 #include "utils/numeric.h"
31 #include "utils/pg_locale.h"
34 /*************************************************************************
36 ************************************************************************/
42 static const char *const small[] = {
43 "zero", "one", "two", "three", "four", "five", "six", "seven",
44 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
45 "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
46 "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
48 const char *const *big = small + 18;
51 /* deal with the simple cases first */
55 /* is it an even multiple of 100? */
58 sprintf(buf, "%s hundred", small[value / 100]);
65 /* is it an even multiple of 10 other than 10? */
66 if (value % 10 == 0 && tu > 10)
67 sprintf(buf, "%s hundred %s",
68 small[value / 100], big[tu / 10]);
70 sprintf(buf, "%s hundred and %s",
71 small[value / 100], small[tu]);
73 sprintf(buf, "%s hundred %s %s",
74 small[value / 100], big[tu / 10], small[tu % 10]);
78 /* is it an even multiple of 10 other than 10? */
79 if (value % 10 == 0 && tu > 10)
80 sprintf(buf, "%s", big[tu / 10]);
82 sprintf(buf, "%s", small[tu]);
84 sprintf(buf, "%s %s", big[tu / 10], small[tu % 10]);
91 * Convert a string to a cash data type.
92 * Format is [$]###[,]###[.##]
93 * Examples: 123.45 $123.45 $123,456.78
97 cash_in(PG_FUNCTION_ARGS)
99 char *str = PG_GETARG_CSTRING(0);
104 bool seen_dot = false;
112 struct lconv *lconvert = PGLC_localeconv();
115 * frac_digits will be CHAR_MAX in some locales, notably C. However, just
116 * testing for == CHAR_MAX is risky, because of compilers like gcc that
117 * "helpfully" let you alter the platform-standard definition of whether
118 * char is signed or not. If we are so unfortunate as to get compiled
119 * with a nonstandard -fsigned-char or -funsigned-char switch, then our
120 * idea of CHAR_MAX will not agree with libc's. The safest course is not
121 * to test for CHAR_MAX at all, but to impose a range check for plausible
122 * frac_digits values.
124 fpoint = lconvert->frac_digits;
125 if (fpoint < 0 || fpoint > 10)
126 fpoint = 2; /* best guess in this case, I think */
128 /* we restrict dsymbol to be a single byte, but not the other symbols */
129 if (*lconvert->mon_decimal_point != '\0' &&
130 lconvert->mon_decimal_point[1] == '\0')
131 dsymbol = *lconvert->mon_decimal_point;
134 if (*lconvert->mon_thousands_sep != '\0')
135 ssymbol = lconvert->mon_thousands_sep;
136 else /* ssymbol should not equal dsymbol */
137 ssymbol = (dsymbol != ',') ? "," : ".";
138 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
139 psymbol = (*lconvert->positive_sign != '\0') ? lconvert->positive_sign : "+";
140 nsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
143 printf("cashin- precision '%d'; decimal '%c'; thousands '%s'; currency '%s'; positive '%s'; negative '%s'\n",
144 fpoint, dsymbol, ssymbol, csymbol, psymbol, nsymbol);
147 /* we need to add all sorts of checking here. For now just */
148 /* strip all leading whitespace and any leading currency symbol */
149 while (isspace((unsigned char) *s))
151 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
152 s += strlen(csymbol);
153 while (isspace((unsigned char) *s))
157 printf("cashin- string is '%s'\n", s);
160 /* a leading minus or paren signifies a negative number */
161 /* again, better heuristics needed */
162 /* XXX - doesn't properly check for balanced parens - djmc */
163 if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
166 s += strlen(nsymbol);
173 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
174 s += strlen(psymbol);
177 printf("cashin- string is '%s'\n", s);
180 /* allow whitespace and currency symbol after the sign, too */
181 while (isspace((unsigned char) *s))
183 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
184 s += strlen(csymbol);
185 while (isspace((unsigned char) *s))
189 printf("cashin- string is '%s'\n", s);
193 * We accumulate the absolute amount in "value" and then apply the sign at
194 * the end. (The sign can appear before or after the digits, so it would
195 * be more complicated to do otherwise.) Because of the larger range of
196 * negative signed integers, we build "value" in the negative and then
197 * flip the sign at the end, catching most-negative-number overflow if
204 * We look for digits as long as we have found less than the required
205 * number of decimal places.
207 if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
209 int8 digit = *s - '0';
211 if (pg_mul_s64_overflow(value, 10, &value) ||
212 pg_sub_s64_overflow(value, digit, &value))
214 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
215 errmsg("value \"%s\" is out of range for type %s",
221 /* decimal point? then start counting fractions... */
222 else if (*s == dsymbol && !seen_dot)
226 /* ignore if "thousands" separator, else we're done */
227 else if (strncmp(s, ssymbol, strlen(ssymbol)) == 0)
228 s += strlen(ssymbol) - 1;
233 /* round off if there's another digit */
234 if (isdigit((unsigned char) *s) && *s >= '5')
236 /* remember we build the value in the negative */
237 if (pg_sub_s64_overflow(value, 1, &value))
239 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
240 errmsg("value \"%s\" is out of range for type %s",
244 /* adjust for less than required decimal places */
245 for (; dec < fpoint; dec++)
247 if (pg_mul_s64_overflow(value, 10, &value))
249 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
250 errmsg("value \"%s\" is out of range for type %s",
255 * should only be trailing digits followed by whitespace, right paren,
256 * trailing sign, and/or trailing currency symbol
258 while (isdigit((unsigned char) *s))
263 if (isspace((unsigned char) *s) || *s == ')')
265 else if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
268 s += strlen(nsymbol);
270 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
271 s += strlen(psymbol);
272 else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
273 s += strlen(csymbol);
276 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
277 errmsg("invalid input syntax for type %s: \"%s\"",
282 * If the value is supposed to be positive, flip the sign, but check for
283 * the most negative number.
287 if (value == PG_INT64_MIN)
289 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
290 errmsg("value \"%s\" is out of range for type %s",
298 printf("cashin- result is " INT64_FORMAT "\n", result);
301 PG_RETURN_CASH(result);
306 * Function to convert cash to a dollars and cents representation, using
307 * the lc_monetary locale's formatting.
310 cash_out(PG_FUNCTION_ARGS)
312 Cash value = PG_GETARG_CASH(0);
326 struct lconv *lconvert = PGLC_localeconv();
328 /* see comments about frac_digits in cash_in() */
329 points = lconvert->frac_digits;
330 if (points < 0 || points > 10)
331 points = 2; /* best guess in this case, I think */
334 * As with frac_digits, must apply a range check to mon_grouping to avoid
335 * being fooled by variant CHAR_MAX values.
337 mon_group = *lconvert->mon_grouping;
338 if (mon_group <= 0 || mon_group > 6)
341 /* we restrict dsymbol to be a single byte, but not the other symbols */
342 if (*lconvert->mon_decimal_point != '\0' &&
343 lconvert->mon_decimal_point[1] == '\0')
344 dsymbol = *lconvert->mon_decimal_point;
347 if (*lconvert->mon_thousands_sep != '\0')
348 ssymbol = lconvert->mon_thousands_sep;
349 else /* ssymbol should not equal dsymbol */
350 ssymbol = (dsymbol != ',') ? "," : ".";
351 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
355 /* make the amount positive for digit-reconstruction loop */
357 /* set up formatting data */
358 signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
359 sign_posn = lconvert->n_sign_posn;
360 cs_precedes = lconvert->n_cs_precedes;
361 sep_by_space = lconvert->n_sep_by_space;
365 signsymbol = lconvert->positive_sign;
366 sign_posn = lconvert->p_sign_posn;
367 cs_precedes = lconvert->p_cs_precedes;
368 sep_by_space = lconvert->p_sep_by_space;
371 /* we build the digits+decimal-point+sep string right-to-left in buf[] */
372 bufptr = buf + sizeof(buf) - 1;
376 * Generate digits till there are no non-zero digits left and we emitted
377 * at least one to the left of the decimal point. digit_pos is the
378 * current digit position, with zero as the digit just left of the decimal
379 * point, increasing to the right.
384 if (points && digit_pos == 0)
386 /* insert decimal point, but not if value cannot be fractional */
387 *(--bufptr) = dsymbol;
389 else if (digit_pos < 0 && (digit_pos % mon_group) == 0)
391 /* insert thousands sep, but only to left of radix point */
392 bufptr -= strlen(ssymbol);
393 memcpy(bufptr, ssymbol, strlen(ssymbol));
396 *(--bufptr) = ((uint64) value % 10) + '0';
397 value = ((uint64) value) / 10;
399 } while (value || digit_pos >= 0);
402 * Now, attach currency symbol and sign symbol in the correct order.
404 * The POSIX spec defines these values controlling this code:
407 * 0 Parentheses enclose the quantity and the currency_symbol.
408 * 1 The sign string precedes the quantity and the currency_symbol.
409 * 2 The sign string succeeds the quantity and the currency_symbol.
410 * 3 The sign string precedes the currency_symbol.
411 * 4 The sign string succeeds the currency_symbol.
413 * p/n_cs_precedes: 0 means currency symbol after value, else before it.
416 * 0 No <space> separates the currency symbol and value.
417 * 1 If the currency symbol and sign string are adjacent, a <space>
418 * separates them from the value; otherwise, a <space> separates
419 * the currency symbol from the value.
420 * 2 If the currency symbol and sign string are adjacent, a <space>
421 * separates them; otherwise, a <space> separates the sign string
429 result = psprintf("(%s%s%s)",
431 (sep_by_space == 1) ? " " : "",
434 result = psprintf("(%s%s%s)",
436 (sep_by_space == 1) ? " " : "",
442 result = psprintf("%s%s%s%s%s",
444 (sep_by_space == 2) ? " " : "",
446 (sep_by_space == 1) ? " " : "",
449 result = psprintf("%s%s%s%s%s",
451 (sep_by_space == 2) ? " " : "",
453 (sep_by_space == 1) ? " " : "",
458 result = psprintf("%s%s%s%s%s",
460 (sep_by_space == 1) ? " " : "",
462 (sep_by_space == 2) ? " " : "",
465 result = psprintf("%s%s%s%s%s",
467 (sep_by_space == 1) ? " " : "",
469 (sep_by_space == 2) ? " " : "",
474 result = psprintf("%s%s%s%s%s",
476 (sep_by_space == 2) ? " " : "",
478 (sep_by_space == 1) ? " " : "",
481 result = psprintf("%s%s%s%s%s",
483 (sep_by_space == 1) ? " " : "",
485 (sep_by_space == 2) ? " " : "",
490 result = psprintf("%s%s%s%s%s",
492 (sep_by_space == 2) ? " " : "",
494 (sep_by_space == 1) ? " " : "",
497 result = psprintf("%s%s%s%s%s",
499 (sep_by_space == 1) ? " " : "",
501 (sep_by_space == 2) ? " " : "",
506 PG_RETURN_CSTRING(result);
510 * cash_recv - converts external binary format to cash
513 cash_recv(PG_FUNCTION_ARGS)
515 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
517 PG_RETURN_CASH((Cash) pq_getmsgint64(buf));
521 * cash_send - converts cash to binary format
524 cash_send(PG_FUNCTION_ARGS)
526 Cash arg1 = PG_GETARG_CASH(0);
529 pq_begintypsend(&buf);
530 pq_sendint64(&buf, arg1);
531 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
535 * Comparison functions
539 cash_eq(PG_FUNCTION_ARGS)
541 Cash c1 = PG_GETARG_CASH(0);
542 Cash c2 = PG_GETARG_CASH(1);
544 PG_RETURN_BOOL(c1 == c2);
548 cash_ne(PG_FUNCTION_ARGS)
550 Cash c1 = PG_GETARG_CASH(0);
551 Cash c2 = PG_GETARG_CASH(1);
553 PG_RETURN_BOOL(c1 != c2);
557 cash_lt(PG_FUNCTION_ARGS)
559 Cash c1 = PG_GETARG_CASH(0);
560 Cash c2 = PG_GETARG_CASH(1);
562 PG_RETURN_BOOL(c1 < c2);
566 cash_le(PG_FUNCTION_ARGS)
568 Cash c1 = PG_GETARG_CASH(0);
569 Cash c2 = PG_GETARG_CASH(1);
571 PG_RETURN_BOOL(c1 <= c2);
575 cash_gt(PG_FUNCTION_ARGS)
577 Cash c1 = PG_GETARG_CASH(0);
578 Cash c2 = PG_GETARG_CASH(1);
580 PG_RETURN_BOOL(c1 > c2);
584 cash_ge(PG_FUNCTION_ARGS)
586 Cash c1 = PG_GETARG_CASH(0);
587 Cash c2 = PG_GETARG_CASH(1);
589 PG_RETURN_BOOL(c1 >= c2);
593 cash_cmp(PG_FUNCTION_ARGS)
595 Cash c1 = PG_GETARG_CASH(0);
596 Cash c2 = PG_GETARG_CASH(1);
608 * Add two cash values.
611 cash_pl(PG_FUNCTION_ARGS)
613 Cash c1 = PG_GETARG_CASH(0);
614 Cash c2 = PG_GETARG_CASH(1);
619 PG_RETURN_CASH(result);
624 * Subtract two cash values.
627 cash_mi(PG_FUNCTION_ARGS)
629 Cash c1 = PG_GETARG_CASH(0);
630 Cash c2 = PG_GETARG_CASH(1);
635 PG_RETURN_CASH(result);
640 * Divide cash by cash, returning float8.
643 cash_div_cash(PG_FUNCTION_ARGS)
645 Cash dividend = PG_GETARG_CASH(0);
646 Cash divisor = PG_GETARG_CASH(1);
651 (errcode(ERRCODE_DIVISION_BY_ZERO),
652 errmsg("division by zero")));
654 quotient = (float8) dividend / (float8) divisor;
655 PG_RETURN_FLOAT8(quotient);
660 * Multiply cash by float8.
663 cash_mul_flt8(PG_FUNCTION_ARGS)
665 Cash c = PG_GETARG_CASH(0);
666 float8 f = PG_GETARG_FLOAT8(1);
669 result = rint(c * f);
670 PG_RETURN_CASH(result);
675 * Multiply float8 by cash.
678 flt8_mul_cash(PG_FUNCTION_ARGS)
680 float8 f = PG_GETARG_FLOAT8(0);
681 Cash c = PG_GETARG_CASH(1);
684 result = rint(f * c);
685 PG_RETURN_CASH(result);
690 * Divide cash by float8.
693 cash_div_flt8(PG_FUNCTION_ARGS)
695 Cash c = PG_GETARG_CASH(0);
696 float8 f = PG_GETARG_FLOAT8(1);
701 (errcode(ERRCODE_DIVISION_BY_ZERO),
702 errmsg("division by zero")));
704 result = rint(c / f);
705 PG_RETURN_CASH(result);
710 * Multiply cash by float4.
713 cash_mul_flt4(PG_FUNCTION_ARGS)
715 Cash c = PG_GETARG_CASH(0);
716 float4 f = PG_GETARG_FLOAT4(1);
719 result = rint(c * (float8) f);
720 PG_RETURN_CASH(result);
725 * Multiply float4 by cash.
728 flt4_mul_cash(PG_FUNCTION_ARGS)
730 float4 f = PG_GETARG_FLOAT4(0);
731 Cash c = PG_GETARG_CASH(1);
734 result = rint((float8) f * c);
735 PG_RETURN_CASH(result);
740 * Divide cash by float4.
744 cash_div_flt4(PG_FUNCTION_ARGS)
746 Cash c = PG_GETARG_CASH(0);
747 float4 f = PG_GETARG_FLOAT4(1);
752 (errcode(ERRCODE_DIVISION_BY_ZERO),
753 errmsg("division by zero")));
755 result = rint(c / (float8) f);
756 PG_RETURN_CASH(result);
761 * Multiply cash by int8.
764 cash_mul_int8(PG_FUNCTION_ARGS)
766 Cash c = PG_GETARG_CASH(0);
767 int64 i = PG_GETARG_INT64(1);
771 PG_RETURN_CASH(result);
776 * Multiply int8 by cash.
779 int8_mul_cash(PG_FUNCTION_ARGS)
781 int64 i = PG_GETARG_INT64(0);
782 Cash c = PG_GETARG_CASH(1);
786 PG_RETURN_CASH(result);
790 * Divide cash by 8-byte integer.
793 cash_div_int8(PG_FUNCTION_ARGS)
795 Cash c = PG_GETARG_CASH(0);
796 int64 i = PG_GETARG_INT64(1);
801 (errcode(ERRCODE_DIVISION_BY_ZERO),
802 errmsg("division by zero")));
806 PG_RETURN_CASH(result);
811 * Multiply cash by int4.
814 cash_mul_int4(PG_FUNCTION_ARGS)
816 Cash c = PG_GETARG_CASH(0);
817 int32 i = PG_GETARG_INT32(1);
821 PG_RETURN_CASH(result);
826 * Multiply int4 by cash.
829 int4_mul_cash(PG_FUNCTION_ARGS)
831 int32 i = PG_GETARG_INT32(0);
832 Cash c = PG_GETARG_CASH(1);
836 PG_RETURN_CASH(result);
841 * Divide cash by 4-byte integer.
845 cash_div_int4(PG_FUNCTION_ARGS)
847 Cash c = PG_GETARG_CASH(0);
848 int32 i = PG_GETARG_INT32(1);
853 (errcode(ERRCODE_DIVISION_BY_ZERO),
854 errmsg("division by zero")));
858 PG_RETURN_CASH(result);
863 * Multiply cash by int2.
866 cash_mul_int2(PG_FUNCTION_ARGS)
868 Cash c = PG_GETARG_CASH(0);
869 int16 s = PG_GETARG_INT16(1);
873 PG_RETURN_CASH(result);
877 * Multiply int2 by cash.
880 int2_mul_cash(PG_FUNCTION_ARGS)
882 int16 s = PG_GETARG_INT16(0);
883 Cash c = PG_GETARG_CASH(1);
887 PG_RETURN_CASH(result);
891 * Divide cash by int2.
895 cash_div_int2(PG_FUNCTION_ARGS)
897 Cash c = PG_GETARG_CASH(0);
898 int16 s = PG_GETARG_INT16(1);
903 (errcode(ERRCODE_DIVISION_BY_ZERO),
904 errmsg("division by zero")));
907 PG_RETURN_CASH(result);
911 * Return larger of two cash values.
914 cashlarger(PG_FUNCTION_ARGS)
916 Cash c1 = PG_GETARG_CASH(0);
917 Cash c2 = PG_GETARG_CASH(1);
920 result = (c1 > c2) ? c1 : c2;
922 PG_RETURN_CASH(result);
926 * Return smaller of two cash values.
929 cashsmaller(PG_FUNCTION_ARGS)
931 Cash c1 = PG_GETARG_CASH(0);
932 Cash c2 = PG_GETARG_CASH(1);
935 result = (c1 < c2) ? c1 : c2;
937 PG_RETURN_CASH(result);
941 * This converts an int4 as well but to a representation using words
942 * Obviously way North American centric - sorry
945 cash_words(PG_FUNCTION_ARGS)
947 Cash value = PG_GETARG_CASH(0);
959 /* work with positive numbers */
963 strcpy(buf, "minus ");
969 /* Now treat as unsigned, to avoid trouble at INT_MIN */
970 val = (uint64) value;
972 m0 = val % INT64CONST(100); /* cents */
973 m1 = (val / INT64CONST(100)) % 1000; /* hundreds */
974 m2 = (val / INT64CONST(100000)) % 1000; /* thousands */
975 m3 = (val / INT64CONST(100000000)) % 1000; /* millions */
976 m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */
977 m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */
978 m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */
982 strcat(buf, num_word(m6));
983 strcat(buf, " quadrillion ");
988 strcat(buf, num_word(m5));
989 strcat(buf, " trillion ");
994 strcat(buf, num_word(m4));
995 strcat(buf, " billion ");
1000 strcat(buf, num_word(m3));
1001 strcat(buf, " million ");
1006 strcat(buf, num_word(m2));
1007 strcat(buf, " thousand ");
1011 strcat(buf, num_word(m1));
1014 strcat(buf, "zero");
1016 strcat(buf, (val / 100) == 1 ? " dollar and " : " dollars and ");
1017 strcat(buf, num_word(m0));
1018 strcat(buf, m0 == 1 ? " cent" : " cents");
1020 /* capitalize output */
1021 buf[0] = pg_toupper((unsigned char) buf[0]);
1023 /* return as text datum */
1024 PG_RETURN_TEXT_P(cstring_to_text(buf));
1029 * Convert cash to numeric.
1032 cash_numeric(PG_FUNCTION_ARGS)
1034 Cash money = PG_GETARG_CASH(0);
1037 struct lconv *lconvert = PGLC_localeconv();
1039 /* see comments about frac_digits in cash_in() */
1040 fpoint = lconvert->frac_digits;
1041 if (fpoint < 0 || fpoint > 10)
1044 /* convert the integral money value to numeric */
1045 result = DirectFunctionCall1(int8_numeric, Int64GetDatum(money));
1047 /* scale appropriately, if needed */
1052 Datum numeric_scale;
1055 /* compute required scale factor */
1057 for (i = 0; i < fpoint; i++)
1059 numeric_scale = DirectFunctionCall1(int8_numeric,
1060 Int64GetDatum(scale));
1063 * Given integral inputs approaching INT64_MAX, select_div_scale()
1064 * might choose a result scale of zero, causing loss of fractional
1065 * digits in the quotient. We can ensure an exact result by setting
1066 * the dscale of either input to be at least as large as the desired
1067 * result scale. numeric_round() will do that for us.
1069 numeric_scale = DirectFunctionCall2(numeric_round,
1071 Int32GetDatum(fpoint));
1073 /* Now we can safely divide ... */
1074 quotient = DirectFunctionCall2(numeric_div, result, numeric_scale);
1076 /* ... and forcibly round to exactly the intended number of digits */
1077 result = DirectFunctionCall2(numeric_round,
1079 Int32GetDatum(fpoint));
1082 PG_RETURN_DATUM(result);
1086 * Convert numeric to cash.
1089 numeric_cash(PG_FUNCTION_ARGS)
1091 Datum amount = PG_GETARG_DATUM(0);
1096 Datum numeric_scale;
1097 struct lconv *lconvert = PGLC_localeconv();
1099 /* see comments about frac_digits in cash_in() */
1100 fpoint = lconvert->frac_digits;
1101 if (fpoint < 0 || fpoint > 10)
1104 /* compute required scale factor */
1106 for (i = 0; i < fpoint; i++)
1109 /* multiply the input amount by scale factor */
1110 numeric_scale = DirectFunctionCall1(int8_numeric, Int64GetDatum(scale));
1111 amount = DirectFunctionCall2(numeric_mul, amount, numeric_scale);
1113 /* note that numeric_int8 will round to nearest integer for us */
1114 result = DatumGetInt64(DirectFunctionCall1(numeric_int8, amount));
1116 PG_RETURN_CASH(result);
1120 * Convert int4 (int) to cash
1123 int4_cash(PG_FUNCTION_ARGS)
1125 int32 amount = PG_GETARG_INT32(0);
1130 struct lconv *lconvert = PGLC_localeconv();
1132 /* see comments about frac_digits in cash_in() */
1133 fpoint = lconvert->frac_digits;
1134 if (fpoint < 0 || fpoint > 10)
1137 /* compute required scale factor */
1139 for (i = 0; i < fpoint; i++)
1142 /* compute amount * scale, checking for overflow */
1143 result = DatumGetInt64(DirectFunctionCall2(int8mul, Int64GetDatum(amount),
1144 Int64GetDatum(scale)));
1146 PG_RETURN_CASH(result);
1150 * Convert int8 (bigint) to cash
1153 int8_cash(PG_FUNCTION_ARGS)
1155 int64 amount = PG_GETARG_INT64(0);
1160 struct lconv *lconvert = PGLC_localeconv();
1162 /* see comments about frac_digits in cash_in() */
1163 fpoint = lconvert->frac_digits;
1164 if (fpoint < 0 || fpoint > 10)
1167 /* compute required scale factor */
1169 for (i = 0; i < fpoint; i++)
1172 /* compute amount * scale, checking for overflow */
1173 result = DatumGetInt64(DirectFunctionCall2(int8mul, Int64GetDatum(amount),
1174 Int64GetDatum(scale)));
1176 PG_RETURN_CASH(result);