PostgreSQL Source Code git master
Loading...
Searching...
No Matches
cash.c
Go to the documentation of this file.
1/*
2 * cash.c
3 * Written by D'Arcy J.M. Cain
4 * darcy@druid.net
5 * http://www.druid.net/darcy/
6 *
7 * Functions to allow input and output of money normally but store
8 * and handle it as 64 bit ints
9 *
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.
15 *
16 * src/backend/utils/adt/cash.c
17 */
18
19#include "postgres.h"
20
21#include <limits.h>
22#include <ctype.h>
23#include <math.h>
24
25#include "common/int.h"
26#include "libpq/pqformat.h"
27#include "nodes/miscnodes.h"
28#include "utils/builtins.h"
29#include "utils/cash.h"
30#include "utils/float.h"
31#include "utils/numeric.h"
32#include "utils/pg_locale.h"
33
34
35/*************************************************************************
36 * Private routines
37 ************************************************************************/
38
39static void
41{
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"
47 };
48 const char *const *big = small + 18;
49 int tu = value % 100;
50
51 /* deal with the simple cases first */
52 if (value <= 20)
53 {
55 return;
56 }
57
58 /* is it an even multiple of 100? */
59 if (!tu)
60 {
61 appendStringInfo(buf, "%s hundred", small[value / 100]);
62 return;
63 }
64
65 /* more than 99? */
66 if (value > 99)
67 {
68 /* is it an even multiple of 10 other than 10? */
69 if (value % 10 == 0 && tu > 10)
70 appendStringInfo(buf, "%s hundred %s",
71 small[value / 100], big[tu / 10]);
72 else if (tu < 20)
73 appendStringInfo(buf, "%s hundred and %s",
74 small[value / 100], small[tu]);
75 else
76 appendStringInfo(buf, "%s hundred %s %s",
77 small[value / 100], big[tu / 10], small[tu % 10]);
78 }
79 else
80 {
81 /* is it an even multiple of 10 other than 10? */
82 if (value % 10 == 0 && tu > 10)
84 else if (tu < 20)
86 else
87 appendStringInfo(buf, "%s %s", big[tu / 10], small[tu % 10]);
88 }
89}
90
91static inline Cash
93{
94 Cash res;
95
96 if (unlikely(pg_add_s64_overflow(c1, c2, &res)))
99 errmsg("money out of range")));
100
101 return res;
102}
103
104static inline Cash
106{
107 Cash res;
108
109 if (unlikely(pg_sub_s64_overflow(c1, c2, &res)))
112 errmsg("money out of range")));
113
114 return res;
115}
116
117static inline Cash
119{
120 float8 res = rint(float8_mul((float8) c, f));
121
122 if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
125 errmsg("money out of range")));
126
127 return (Cash) res;
128}
129
130static inline Cash
132{
133 float8 res = rint(float8_div((float8) c, f));
134
135 if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
138 errmsg("money out of range")));
139
140 return (Cash) res;
141}
142
143static inline Cash
145{
146 Cash res;
147
148 if (unlikely(pg_mul_s64_overflow(c, i, &res)))
151 errmsg("money out of range")));
152
153 return res;
154}
155
156static inline Cash
158{
159 if (unlikely(i == 0))
162 errmsg("division by zero")));
163
164 return c / i;
165}
166
167/* cash_in()
168 * Convert a string to a cash data type.
169 * Format is [$]###[,]###[.##]
170 * Examples: 123.45 $123.45 $123,456.78
171 *
172 */
173Datum
175{
176 char *str = PG_GETARG_CSTRING(0);
177 Node *escontext = fcinfo->context;
178 Cash result;
179 Cash value = 0;
180 Cash dec = 0;
181 Cash sgn = 1;
182 bool seen_dot = false;
183 const char *s = str;
184 int fpoint;
185 char dsymbol;
186 const char *ssymbol,
187 *psymbol,
188 *nsymbol,
189 *csymbol;
190 struct lconv *lconvert = PGLC_localeconv();
191
192 /*
193 * frac_digits will be CHAR_MAX in some locales, notably C. However, just
194 * testing for == CHAR_MAX is risky, because of compilers like gcc that
195 * "helpfully" let you alter the platform-standard definition of whether
196 * char is signed or not. If we are so unfortunate as to get compiled
197 * with a nonstandard -fsigned-char or -funsigned-char switch, then our
198 * idea of CHAR_MAX will not agree with libc's. The safest course is not
199 * to test for CHAR_MAX at all, but to impose a range check for plausible
200 * frac_digits values.
201 */
202 fpoint = lconvert->frac_digits;
204 fpoint = 2; /* best guess in this case, I think */
205
206 /* we restrict dsymbol to be a single byte, but not the other symbols */
207 if (*lconvert->mon_decimal_point != '\0' &&
208 lconvert->mon_decimal_point[1] == '\0')
209 dsymbol = *lconvert->mon_decimal_point;
210 else
211 dsymbol = '.';
212 if (*lconvert->mon_thousands_sep != '\0')
213 ssymbol = lconvert->mon_thousands_sep;
214 else /* ssymbol should not equal dsymbol */
215 ssymbol = (dsymbol != ',') ? "," : ".";
216 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
217 psymbol = (*lconvert->positive_sign != '\0') ? lconvert->positive_sign : "+";
218 nsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
219
220#ifdef CASHDEBUG
221 printf("cashin- precision '%d'; decimal '%c'; thousands '%s'; currency '%s'; positive '%s'; negative '%s'\n",
223#endif
224
225 /* we need to add all sorts of checking here. For now just */
226 /* strip all leading whitespace and any leading currency symbol */
227 while (isspace((unsigned char) *s))
228 s++;
229 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
230 s += strlen(csymbol);
231 while (isspace((unsigned char) *s))
232 s++;
233
234#ifdef CASHDEBUG
235 printf("cashin- string is '%s'\n", s);
236#endif
237
238 /* a leading minus or paren signifies a negative number */
239 /* again, better heuristics needed */
240 /* XXX - doesn't properly check for balanced parens - djmc */
241 if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
242 {
243 sgn = -1;
244 s += strlen(nsymbol);
245 }
246 else if (*s == '(')
247 {
248 sgn = -1;
249 s++;
250 }
251 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
252 s += strlen(psymbol);
253
254#ifdef CASHDEBUG
255 printf("cashin- string is '%s'\n", s);
256#endif
257
258 /* allow whitespace and currency symbol after the sign, too */
259 while (isspace((unsigned char) *s))
260 s++;
261 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
262 s += strlen(csymbol);
263 while (isspace((unsigned char) *s))
264 s++;
265
266#ifdef CASHDEBUG
267 printf("cashin- string is '%s'\n", s);
268#endif
269
270 /*
271 * We accumulate the absolute amount in "value" and then apply the sign at
272 * the end. (The sign can appear before or after the digits, so it would
273 * be more complicated to do otherwise.) Because of the larger range of
274 * negative signed integers, we build "value" in the negative and then
275 * flip the sign at the end, catching most-negative-number overflow if
276 * necessary.
277 */
278
279 for (; *s; s++)
280 {
281 /*
282 * We look for digits as long as we have found less than the required
283 * number of decimal places.
284 */
285 if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
286 {
287 int8 digit = *s - '0';
288
289 if (pg_mul_s64_overflow(value, 10, &value) ||
291 ereturn(escontext, (Datum) 0,
293 errmsg("value \"%s\" is out of range for type %s",
294 str, "money")));
295
296 if (seen_dot)
297 dec++;
298 }
299 /* decimal point? then start counting fractions... */
300 else if (*s == dsymbol && !seen_dot)
301 {
302 seen_dot = true;
303 }
304 /* ignore if "thousands" separator, else we're done */
305 else if (strncmp(s, ssymbol, strlen(ssymbol)) == 0)
306 s += strlen(ssymbol) - 1;
307 else
308 break;
309 }
310
311 /* round off if there's another digit */
312 if (isdigit((unsigned char) *s) && *s >= '5')
313 {
314 /* remember we build the value in the negative */
316 ereturn(escontext, (Datum) 0,
318 errmsg("value \"%s\" is out of range for type %s",
319 str, "money")));
320 }
321
322 /* adjust for less than required decimal places */
323 for (; dec < fpoint; dec++)
324 {
326 ereturn(escontext, (Datum) 0,
328 errmsg("value \"%s\" is out of range for type %s",
329 str, "money")));
330 }
331
332 /*
333 * should only be trailing digits followed by whitespace, right paren,
334 * trailing sign, and/or trailing currency symbol
335 */
336 while (isdigit((unsigned char) *s))
337 s++;
338
339 while (*s)
340 {
341 if (isspace((unsigned char) *s) || *s == ')')
342 s++;
343 else if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
344 {
345 sgn = -1;
346 s += strlen(nsymbol);
347 }
348 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
349 s += strlen(psymbol);
350 else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
351 s += strlen(csymbol);
352 else
353 ereturn(escontext, (Datum) 0,
355 errmsg("invalid input syntax for type %s: \"%s\"",
356 "money", str)));
357 }
358
359 /*
360 * If the value is supposed to be positive, flip the sign, but check for
361 * the most negative number.
362 */
363 if (sgn > 0)
364 {
365 if (value == PG_INT64_MIN)
366 ereturn(escontext, (Datum) 0,
368 errmsg("value \"%s\" is out of range for type %s",
369 str, "money")));
370 result = -value;
371 }
372 else
373 result = value;
374
375#ifdef CASHDEBUG
376 printf("cashin- result is " INT64_FORMAT "\n", result);
377#endif
378
379 PG_RETURN_CASH(result);
380}
381
382
383/* cash_out()
384 * Function to convert cash to a dollars and cents representation, using
385 * the lc_monetary locale's formatting.
386 */
387Datum
389{
392 char *result;
393 char buf[128];
394 char *bufptr;
395 int digit_pos;
396 int points,
397 mon_group;
398 char dsymbol;
399 const char *ssymbol,
400 *csymbol,
401 *signsymbol;
402 char sign_posn,
405 struct lconv *lconvert = PGLC_localeconv();
406
407 /* see comments about frac_digits in cash_in() */
408 points = lconvert->frac_digits;
410 points = 2; /* best guess in this case, I think */
411
412 /*
413 * As with frac_digits, must apply a range check to mon_grouping to avoid
414 * being fooled by variant CHAR_MAX values.
415 */
416 mon_group = *lconvert->mon_grouping;
418 mon_group = 3;
419
420 /* we restrict dsymbol to be a single byte, but not the other symbols */
421 if (*lconvert->mon_decimal_point != '\0' &&
422 lconvert->mon_decimal_point[1] == '\0')
423 dsymbol = *lconvert->mon_decimal_point;
424 else
425 dsymbol = '.';
426 if (*lconvert->mon_thousands_sep != '\0')
427 ssymbol = lconvert->mon_thousands_sep;
428 else /* ssymbol should not equal dsymbol */
429 ssymbol = (dsymbol != ',') ? "," : ".";
430 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
431
432 if (value < 0)
433 {
434 /* set up formatting data */
435 signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
436 sign_posn = lconvert->n_sign_posn;
437 cs_precedes = lconvert->n_cs_precedes;
438 sep_by_space = lconvert->n_sep_by_space;
439 }
440 else
441 {
442 signsymbol = lconvert->positive_sign;
443 sign_posn = lconvert->p_sign_posn;
444 cs_precedes = lconvert->p_cs_precedes;
445 sep_by_space = lconvert->p_sep_by_space;
446 }
447
448 /* make the amount positive for digit-reconstruction loop */
450
451 /* we build the digits+decimal-point+sep string right-to-left in buf[] */
452 bufptr = buf + sizeof(buf) - 1;
453 *bufptr = '\0';
454
455 /*
456 * Generate digits till there are no non-zero digits left and we emitted
457 * at least one to the left of the decimal point. digit_pos is the
458 * current digit position, with zero as the digit just left of the decimal
459 * point, increasing to the right.
460 */
462 do
463 {
464 if (points && digit_pos == 0)
465 {
466 /* insert decimal point, but not if value cannot be fractional */
467 *(--bufptr) = dsymbol;
468 }
469 else if (digit_pos < 0 && (digit_pos % mon_group) == 0)
470 {
471 /* insert thousands sep, but only to left of radix point */
472 bufptr -= strlen(ssymbol);
473 memcpy(bufptr, ssymbol, strlen(ssymbol));
474 }
475
476 *(--bufptr) = (uvalue % 10) + '0';
477 uvalue = uvalue / 10;
478 digit_pos--;
479 } while (uvalue || digit_pos >= 0);
480
481 /*----------
482 * Now, attach currency symbol and sign symbol in the correct order.
483 *
484 * The POSIX spec defines these values controlling this code:
485 *
486 * p/n_sign_posn:
487 * 0 Parentheses enclose the quantity and the currency_symbol.
488 * 1 The sign string precedes the quantity and the currency_symbol.
489 * 2 The sign string succeeds the quantity and the currency_symbol.
490 * 3 The sign string precedes the currency_symbol.
491 * 4 The sign string succeeds the currency_symbol.
492 *
493 * p/n_cs_precedes: 0 means currency symbol after value, else before it.
494 *
495 * p/n_sep_by_space:
496 * 0 No <space> separates the currency symbol and value.
497 * 1 If the currency symbol and sign string are adjacent, a <space>
498 * separates them from the value; otherwise, a <space> separates
499 * the currency symbol from the value.
500 * 2 If the currency symbol and sign string are adjacent, a <space>
501 * separates them; otherwise, a <space> separates the sign string
502 * from the value.
503 *----------
504 */
505 switch (sign_posn)
506 {
507 case 0:
508 if (cs_precedes)
509 result = psprintf("(%s%s%s)",
510 csymbol,
511 (sep_by_space == 1) ? " " : "",
512 bufptr);
513 else
514 result = psprintf("(%s%s%s)",
515 bufptr,
516 (sep_by_space == 1) ? " " : "",
517 csymbol);
518 break;
519 case 1:
520 default:
521 if (cs_precedes)
522 result = psprintf("%s%s%s%s%s",
524 (sep_by_space == 2) ? " " : "",
525 csymbol,
526 (sep_by_space == 1) ? " " : "",
527 bufptr);
528 else
529 result = psprintf("%s%s%s%s%s",
531 (sep_by_space == 2) ? " " : "",
532 bufptr,
533 (sep_by_space == 1) ? " " : "",
534 csymbol);
535 break;
536 case 2:
537 if (cs_precedes)
538 result = psprintf("%s%s%s%s%s",
539 csymbol,
540 (sep_by_space == 1) ? " " : "",
541 bufptr,
542 (sep_by_space == 2) ? " " : "",
543 signsymbol);
544 else
545 result = psprintf("%s%s%s%s%s",
546 bufptr,
547 (sep_by_space == 1) ? " " : "",
548 csymbol,
549 (sep_by_space == 2) ? " " : "",
550 signsymbol);
551 break;
552 case 3:
553 if (cs_precedes)
554 result = psprintf("%s%s%s%s%s",
556 (sep_by_space == 2) ? " " : "",
557 csymbol,
558 (sep_by_space == 1) ? " " : "",
559 bufptr);
560 else
561 result = psprintf("%s%s%s%s%s",
562 bufptr,
563 (sep_by_space == 1) ? " " : "",
565 (sep_by_space == 2) ? " " : "",
566 csymbol);
567 break;
568 case 4:
569 if (cs_precedes)
570 result = psprintf("%s%s%s%s%s",
571 csymbol,
572 (sep_by_space == 2) ? " " : "",
574 (sep_by_space == 1) ? " " : "",
575 bufptr);
576 else
577 result = psprintf("%s%s%s%s%s",
578 bufptr,
579 (sep_by_space == 1) ? " " : "",
580 csymbol,
581 (sep_by_space == 2) ? " " : "",
582 signsymbol);
583 break;
584 }
585
586 PG_RETURN_CSTRING(result);
587}
588
589/*
590 * cash_recv - converts external binary format to cash
591 */
592Datum
599
600/*
601 * cash_send - converts cash to binary format
602 */
603Datum
613
614/*
615 * Comparison functions
616 */
617
618Datum
626
627Datum
635
636Datum
644
645Datum
653
654Datum
662
663Datum
671
672Datum
674{
677
678 if (c1 > c2)
680 else if (c1 == c2)
682 else
683 PG_RETURN_INT32(-1);
684}
685
686
687/* cash_pl()
688 * Add two cash values.
689 */
690Datum
698
699
700/* cash_mi()
701 * Subtract two cash values.
702 */
703Datum
711
712
713/* cash_div_cash()
714 * Divide cash by cash, returning float8.
715 */
716Datum
731
732
733/* cash_mul_flt8()
734 * Multiply cash by float8.
735 */
736Datum
744
745
746/* flt8_mul_cash()
747 * Multiply float8 by cash.
748 */
749Datum
757
758
759/* cash_div_flt8()
760 * Divide cash by float8.
761 */
762Datum
770
771
772/* cash_mul_flt4()
773 * Multiply cash by float4.
774 */
775Datum
783
784
785/* flt4_mul_cash()
786 * Multiply float4 by cash.
787 */
788Datum
796
797
798/* cash_div_flt4()
799 * Divide cash by float4.
800 *
801 */
802Datum
810
811
812/* cash_mul_int8()
813 * Multiply cash by int8.
814 */
815Datum
823
824
825/* int8_mul_cash()
826 * Multiply int8 by cash.
827 */
828Datum
836
837/* cash_div_int8()
838 * Divide cash by 8-byte integer.
839 */
840Datum
848
849
850/* cash_mul_int4()
851 * Multiply cash by int4.
852 */
853Datum
861
862
863/* int4_mul_cash()
864 * Multiply int4 by cash.
865 */
866Datum
874
875
876/* cash_div_int4()
877 * Divide cash by 4-byte integer.
878 *
879 */
880Datum
888
889
890/* cash_mul_int2()
891 * Multiply cash by int2.
892 */
893Datum
901
902/* int2_mul_cash()
903 * Multiply int2 by cash.
904 */
905Datum
913
914/* cash_div_int2()
915 * Divide cash by int2.
916 *
917 */
918Datum
926
927/* cashlarger()
928 * Return larger of two cash values.
929 */
930Datum
932{
935 Cash result;
936
937 result = (c1 > c2) ? c1 : c2;
938
939 PG_RETURN_CASH(result);
940}
941
942/* cashsmaller()
943 * Return smaller of two cash values.
944 */
945Datum
947{
950 Cash result;
951
952 result = (c1 < c2) ? c1 : c2;
953
954 PG_RETURN_CASH(result);
955}
956
957/* cash_words()
958 * This converts an int4 as well but to a representation using words
959 * Obviously way North American centric - sorry
960 */
961Datum
963{
965 uint64 val;
967 text *res;
969 Cash m0;
970 Cash m1;
971 Cash m2;
972 Cash m3;
973 Cash m4;
974 Cash m5;
975 Cash m6;
976
978
979 /* work with positive numbers */
980 if (value < 0)
981 {
982 value = -value;
983 appendStringInfoString(&buf, "minus ");
984 }
985
986 /* Now treat as unsigned, to avoid trouble at INT_MIN */
987 val = (uint64) value;
988
989 dollars = val / INT64CONST(100);
990 m0 = val % INT64CONST(100); /* cents */
991 m1 = (val / INT64CONST(100)) % 1000; /* hundreds */
992 m2 = (val / INT64CONST(100000)) % 1000; /* thousands */
993 m3 = (val / INT64CONST(100000000)) % 1000; /* millions */
994 m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */
995 m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */
996 m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */
997
998 if (m6)
999 {
1001 appendStringInfoString(&buf, " quadrillion ");
1002 }
1003
1004 if (m5)
1005 {
1007 appendStringInfoString(&buf, " trillion ");
1008 }
1009
1010 if (m4)
1011 {
1013 appendStringInfoString(&buf, " billion ");
1014 }
1015
1016 if (m3)
1017 {
1019 appendStringInfoString(&buf, " million ");
1020 }
1021
1022 if (m2)
1023 {
1025 appendStringInfoString(&buf, " thousand ");
1026 }
1027
1028 if (m1)
1030
1031 if (dollars == 0)
1032 appendStringInfoString(&buf, "zero");
1033
1034 appendStringInfoString(&buf, dollars == 1 ? " dollar and " : " dollars and ");
1036 appendStringInfoString(&buf, m0 == 1 ? " cent" : " cents");
1037
1038 /* capitalize output */
1039 buf.data[0] = pg_ascii_toupper((unsigned char) buf.data[0]);
1040
1041 /* return as text datum */
1042 res = cstring_to_text_with_len(buf.data, buf.len);
1043 pfree(buf.data);
1044 PG_RETURN_TEXT_P(res);
1045}
1046
1047
1048/* cash_numeric()
1049 * Convert cash to numeric.
1050 */
1051Datum
1053{
1055 Datum result;
1056 int fpoint;
1057 struct lconv *lconvert = PGLC_localeconv();
1058
1059 /* see comments about frac_digits in cash_in() */
1060 fpoint = lconvert->frac_digits;
1062 fpoint = 2;
1063
1064 /* convert the integral money value to numeric */
1066
1067 /* scale appropriately, if needed */
1068 if (fpoint > 0)
1069 {
1070 int64 scale;
1071 int i;
1074
1075 /* compute required scale factor */
1076 scale = 1;
1077 for (i = 0; i < fpoint; i++)
1078 scale *= 10;
1080
1081 /*
1082 * Given integral inputs approaching INT64_MAX, select_div_scale()
1083 * might choose a result scale of zero, causing loss of fractional
1084 * digits in the quotient. We can ensure an exact result by setting
1085 * the dscale of either input to be at least as large as the desired
1086 * result scale. numeric_round() will do that for us.
1087 */
1091
1092 /* Now we can safely divide ... */
1094
1095 /* ... and forcibly round to exactly the intended number of digits */
1097 quotient,
1099 }
1100
1101 PG_RETURN_DATUM(result);
1102}
1103
1104/* numeric_cash()
1105 * Convert numeric to cash.
1106 */
1107Datum
1109{
1111 Cash result;
1112 int fpoint;
1113 int64 scale;
1114 int i;
1116 struct lconv *lconvert = PGLC_localeconv();
1117
1118 /* see comments about frac_digits in cash_in() */
1119 fpoint = lconvert->frac_digits;
1121 fpoint = 2;
1122
1123 /* compute required scale factor */
1124 scale = 1;
1125 for (i = 0; i < fpoint; i++)
1126 scale *= 10;
1127
1128 /* multiply the input amount by scale factor */
1130
1131 amount = numeric_mul_safe(amount, numeric_scale, fcinfo->context);
1132 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1134
1135 /* note that numeric_int8 will round to nearest integer for us */
1136 result = numeric_int8_safe(amount, fcinfo->context);
1137 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1139
1140 PG_RETURN_CASH(result);
1141}
1142
1143/* int4_cash()
1144 * Convert int4 (int) to cash
1145 */
1146Datum
1148{
1150 Cash result;
1151 int fpoint;
1152 int64 scale;
1153 int i;
1154 struct lconv *lconvert = PGLC_localeconv();
1155
1156 /* see comments about frac_digits in cash_in() */
1157 fpoint = lconvert->frac_digits;
1159 fpoint = 2;
1160
1161 /* compute required scale factor */
1162 scale = 1;
1163 for (i = 0; i < fpoint; i++)
1164 scale *= 10;
1165
1166 /* compute amount * scale, checking for overflow */
1167 if (unlikely(pg_mul_s64_overflow(amount, scale, &result)))
1168 ereturn(fcinfo->context, (Datum) 0,
1170 errmsg("bigint out of range"));
1171
1172 PG_RETURN_CASH(result);
1173}
1174
1175/* int8_cash()
1176 * Convert int8 (bigint) to cash
1177 */
1178Datum
1180{
1182 Cash result;
1183 int fpoint;
1184 int64 scale;
1185 int i;
1186 struct lconv *lconvert = PGLC_localeconv();
1187
1188 /* see comments about frac_digits in cash_in() */
1189 fpoint = lconvert->frac_digits;
1191 fpoint = 2;
1192
1193 /* compute required scale factor */
1194 scale = 1;
1195 for (i = 0; i < fpoint; i++)
1196 scale *= 10;
1197
1198 /* compute amount * scale, checking for overflow */
1199 if (unlikely(pg_mul_s64_overflow(amount, scale, &result)))
1200 ereturn(fcinfo->context, (Datum) 0,
1202 errmsg("bigint out of range"));
1203
1204 PG_RETURN_CASH(result);
1205}
Datum numeric_div(PG_FUNCTION_ARGS)
Definition numeric.c:3139
Datum numeric_round(PG_FUNCTION_ARGS)
Definition numeric.c:1528
Numeric int64_to_numeric(int64 val)
Definition numeric.c:4264
int64 numeric_int8_safe(Numeric num, Node *escontext)
Definition numeric.c:4445
Datum numeric_scale(PG_FUNCTION_ARGS)
Definition numeric.c:4103
Numeric numeric_mul_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:3038
#define INT64CONST(x)
Definition c.h:632
#define INT64_FORMAT
Definition c.h:636
int64_t int64
Definition c.h:615
double float8
Definition c.h:716
int16_t int16
Definition c.h:613
int8_t int8
Definition c.h:612
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1181
int32_t int32
Definition c.h:614
#define PG_INT64_MIN
Definition c.h:677
uint64_t uint64
Definition c.h:619
#define unlikely(x)
Definition c.h:432
float float4
Definition c.h:715
Datum int8_cash(PG_FUNCTION_ARGS)
Definition cash.c:1179
Datum cash_mul_int8(PG_FUNCTION_ARGS)
Definition cash.c:816
Datum flt8_mul_cash(PG_FUNCTION_ARGS)
Definition cash.c:750
static Cash cash_mi_cash(Cash c1, Cash c2)
Definition cash.c:105
Datum cash_mi(PG_FUNCTION_ARGS)
Definition cash.c:704
Datum flt4_mul_cash(PG_FUNCTION_ARGS)
Definition cash.c:789
Datum cash_numeric(PG_FUNCTION_ARGS)
Definition cash.c:1052
static Cash cash_div_int64(Cash c, int64 i)
Definition cash.c:157
Datum cash_gt(PG_FUNCTION_ARGS)
Definition cash.c:655
Datum cash_div_cash(PG_FUNCTION_ARGS)
Definition cash.c:717
Datum cash_mul_flt4(PG_FUNCTION_ARGS)
Definition cash.c:776
Datum cash_ne(PG_FUNCTION_ARGS)
Definition cash.c:628
Datum cash_out(PG_FUNCTION_ARGS)
Definition cash.c:388
static Cash cash_mul_int64(Cash c, int64 i)
Definition cash.c:144
static Cash cash_mul_float8(Cash c, float8 f)
Definition cash.c:118
Datum cash_ge(PG_FUNCTION_ARGS)
Definition cash.c:664
Datum cash_div_int4(PG_FUNCTION_ARGS)
Definition cash.c:881
Datum cash_in(PG_FUNCTION_ARGS)
Definition cash.c:174
Datum numeric_cash(PG_FUNCTION_ARGS)
Definition cash.c:1108
Datum cash_pl(PG_FUNCTION_ARGS)
Definition cash.c:691
Datum cash_eq(PG_FUNCTION_ARGS)
Definition cash.c:619
static Cash cash_div_float8(Cash c, float8 f)
Definition cash.c:131
Datum cash_div_int2(PG_FUNCTION_ARGS)
Definition cash.c:919
static void append_num_word(StringInfo buf, Cash value)
Definition cash.c:40
Datum cash_mul_flt8(PG_FUNCTION_ARGS)
Definition cash.c:737
Datum int4_mul_cash(PG_FUNCTION_ARGS)
Definition cash.c:867
Datum int2_mul_cash(PG_FUNCTION_ARGS)
Definition cash.c:906
Datum cash_div_int8(PG_FUNCTION_ARGS)
Definition cash.c:841
Datum cash_lt(PG_FUNCTION_ARGS)
Definition cash.c:637
Datum cashlarger(PG_FUNCTION_ARGS)
Definition cash.c:931
Datum cash_send(PG_FUNCTION_ARGS)
Definition cash.c:604
Datum cash_mul_int4(PG_FUNCTION_ARGS)
Definition cash.c:854
static Cash cash_pl_cash(Cash c1, Cash c2)
Definition cash.c:92
Datum cash_recv(PG_FUNCTION_ARGS)
Definition cash.c:593
Datum cash_div_flt8(PG_FUNCTION_ARGS)
Definition cash.c:763
Datum cashsmaller(PG_FUNCTION_ARGS)
Definition cash.c:946
Datum cash_le(PG_FUNCTION_ARGS)
Definition cash.c:646
Datum cash_div_flt4(PG_FUNCTION_ARGS)
Definition cash.c:803
Datum int8_mul_cash(PG_FUNCTION_ARGS)
Definition cash.c:829
Datum cash_cmp(PG_FUNCTION_ARGS)
Definition cash.c:673
Datum cash_mul_int2(PG_FUNCTION_ARGS)
Definition cash.c:894
Datum int4_cash(PG_FUNCTION_ARGS)
Definition cash.c:1147
Datum cash_words(PG_FUNCTION_ARGS)
Definition cash.c:962
int64 Cash
Definition cash.h:17
#define PG_RETURN_CASH(x)
Definition cash.h:33
#define PG_GETARG_CASH(n)
Definition cash.h:32
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereturn(context, dummy_value,...)
Definition elog.h:278
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static float8 float8_mul(const float8 val1, const float8 val2)
Definition float.h:192
static float8 float8_div(const float8 val1, const float8 val2)
Definition float.h:230
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define PG_GETARG_INT16(n)
Definition fmgr.h:271
const char * str
long val
Definition informix.c:689
static struct @175 value
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
static uint64 pg_abs_s64(int64 a)
Definition int.h:352
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
#define PG_GETARG_NUMERIC(n)
Definition numeric.h:81
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76
static char * errmsg
struct lconv * PGLC_localeconv(void)
Definition pg_locale.c:506
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int scale
Definition pgbench.c:182
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition port.h:177
#define printf(...)
Definition port.h:266
uint64_t Datum
Definition postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint64(StringInfo buf, uint64 i)
Definition pqformat.h:152
char * c
static int fb(int x)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
struct StringInfoData * StringInfo
Definition string.h:15
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition nodes.h:135
Definition c.h:778
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:196