📄 cash.c
字号:
if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't subtract cash", NULL); *result = (*c1 - *c2); return result;} /* cash_mi() *//* cash_mul_flt8() * Multiply cash by float8. */Cash *cash_mul_flt8(Cash *c, float8 *f){ Cash *result; if (!PointerIsValid(f) || !PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't multiply cash", NULL); *result = ((*f) * (*c)); return result;} /* cash_mul_flt8() *//* flt8_mul_cash() * Multiply float8 by cash. */Cash *flt8_mul_cash(float8 *f, Cash *c){ return cash_mul_flt8(c, f);} /* flt8_mul_cash() *//* cash_div_flt8() * Divide cash by float8. * * XXX Don't know if rounding or truncating is correct behavior. * Round for now. - tgl 97/04/15 */Cash *cash_div_flt8(Cash *c, float8 *f){ Cash *result; if (!PointerIsValid(f) || !PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't divide cash", NULL); if (*f == 0.0) elog(ERROR, "cash_div: divide by 0.0 error"); *result = rint(*c / *f); return result;} /* cash_div_flt8() *//* cash_mul_flt4() * Multiply cash by float4. */Cash *cash_mul_flt4(Cash *c, float4 *f){ Cash *result; if (!PointerIsValid(f) || !PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't multiply cash", NULL); *result = ((*f) * (*c)); return result;} /* cash_mul_flt4() *//* flt4_mul_cash() * Multiply float4 by float4. */Cash *flt4_mul_cash(float4 *f, Cash *c){ return cash_mul_flt4(c, f);} /* flt4_mul_cash() *//* cash_div_flt4() * Divide cash by float4. * * XXX Don't know if rounding or truncating is correct behavior. * Round for now. - tgl 97/04/15 */Cash *cash_div_flt4(Cash *c, float4 *f){ Cash *result; if (!PointerIsValid(f) || !PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't divide cash", NULL); if (*f == 0.0) elog(ERROR, "cash_div: divide by 0.0 error"); *result = rint(*c / *f); return result;} /* cash_div_flt4() *//* cash_mul_int4() * Multiply cash by int4. */Cash *cash_mul_int4(Cash *c, int4 i){ Cash *result; if (!PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't multiply cash", NULL); *result = ((i) * (*c)); return result;} /* cash_mul_int4() *//* int4_mul_cash() * Multiply int4 by cash. */Cash *int4_mul_cash(int4 i, Cash *c){ return cash_mul_int4(c, i);} /* int4_mul_cash() *//* cash_div_int4() * Divide cash by 4-byte integer. * * XXX Don't know if rounding or truncating is correct behavior. * Round for now. - tgl 97/04/15 */Cash *cash_div_int4(Cash *c, int4 i){ Cash *result; if (!PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't divide cash", NULL); if (i == 0) elog(ERROR, "cash_idiv: divide by 0 error"); *result = rint(*c / i); return result;} /* cash_div_int4() *//* cash_mul_int2() * Multiply cash by int2. */Cash *cash_mul_int2(Cash *c, int2 s){ Cash *result; if (!PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't multiply cash", NULL); *result = ((s) * (*c)); return result;} /* cash_mul_int2() *//* int2_mul_cash() * Multiply int2 by cash. */Cash *int2_mul_cash(int2 s, Cash *c){ return cash_mul_int2(c, s);} /* int2_mul_cash() *//* cash_div_int2() * Divide cash by int2. * * XXX Don't know if rounding or truncating is correct behavior. * Round for now. - tgl 97/04/15 */Cash *cash_div_int2(Cash *c, int2 s){ Cash *result; if (!PointerIsValid(c)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't divide cash", NULL); if (s == 0) elog(ERROR, "cash_div: divide by 0 error"); *result = rint(*c / s); return result;} /* cash_div_int2() *//* cashlarger() * Return larger of two cash values. */Cash *cashlarger(Cash *c1, Cash *c2){ Cash *result; if (!PointerIsValid(c1) || !PointerIsValid(c2)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't return larger cash", NULL); *result = ((*c1 > *c2) ? *c1 : *c2); return result;} /* cashlarger() *//* cashsmaller() * Return smaller of two cash values. */Cash *cashsmaller(Cash *c1, Cash *c2){ Cash *result; if (!PointerIsValid(c1) || !PointerIsValid(c2)) return NULL; if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed, can't return smaller cash", NULL); *result = ((*c1 < *c2) ? *c1 : *c2); return result;} /* cashsmaller() *//* cash_words_out() * This converts a int4 as well but to a representation using words * Obviously way North American centric - sorry */text *cash_words_out(Cash *value){ static char buf[128]; char *p = buf; Cash m0; Cash m1; Cash m2; Cash m3; text *result; /* work with positive numbers */ if (*value < 0) { *value *= -1; strcpy(buf, "minus "); p += 6; } else *buf = 0; m0 = *value % 100; /* cents */ m1 = (*value / 100) % 1000; /* hundreds */ m2 = (*value / 100000) % 1000; /* thousands */ m3 = *value / 100000000 % 1000; /* millions */ if (m3) { strcat(buf, num_word(m3)); strcat(buf, " million "); } if (m2) { strcat(buf, num_word(m2)); strcat(buf, " thousand "); } if (m1) strcat(buf, num_word(m1)); if (!*p) strcat(buf, "zero"); strcat(buf, (int) (*value / 100) == 1 ? " dollar and " : " dollars and "); strcat(buf, num_word(m0)); strcat(buf, m0 == 1 ? " cent" : " cents"); /* capitalize output */ *buf = toupper(*buf); /* make a text type for output */ result = (text *) palloc(strlen(buf) + VARHDRSZ); VARSIZE(result) = strlen(buf) + VARHDRSZ; StrNCpy(VARDATA(result), buf, strlen(buf)); return result;} /* cash_words_out() *//************************************************************************* * Private routines ************************************************************************/static const char *num_word(Cash value){ static char buf[128]; static const char *small[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; const char **big = small + 18; int tu = value % 100; /* deal with the simple cases first */ if (value <= 20) return small[value]; /* is it an even multiple of 100? */ if (!tu) { sprintf(buf, "%s hundred", small[value / 100]); return buf; } /* more than 99? */ if (value > 99) { /* is it an even multiple of 10 other than 10? */ if (value % 10 == 0 && tu > 10) sprintf(buf, "%s hundred %s", small[value / 100], big[tu / 10]); else if (tu < 20) sprintf(buf, "%s hundred and %s", small[value / 100], small[tu]); else sprintf(buf, "%s hundred %s %s", small[value / 100], big[tu / 10], small[tu % 10]); } else { /* is it an even multiple of 10 other than 10? */ if (value % 10 == 0 && tu > 10) sprintf(buf, "%s", big[tu / 10]); else if (tu < 20) sprintf(buf, "%s", small[tu]); else sprintf(buf, "%s %s", big[tu / 10], small[tu % 10]); } return buf;} /* num_word() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -