⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cash.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (c1 > c2)		PG_RETURN_INT32(1);	else if (c1 == c2)		PG_RETURN_INT32(0);	else		PG_RETURN_INT32(-1);}/* cash_pl() * Add two cash values. */Datumcash_pl(PG_FUNCTION_ARGS){	Cash		c1 = PG_GETARG_CASH(0);	Cash		c2 = PG_GETARG_CASH(1);	Cash		result;	result = c1 + c2;	PG_RETURN_CASH(result);}/* cash_mi() * Subtract two cash values. */Datumcash_mi(PG_FUNCTION_ARGS){	Cash		c1 = PG_GETARG_CASH(0);	Cash		c2 = PG_GETARG_CASH(1);	Cash		result;	result = c1 - c2;	PG_RETURN_CASH(result);}/* cash_mul_flt8() * Multiply cash by float8. */Datumcash_mul_flt8(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	float8		f = PG_GETARG_FLOAT8(1);	Cash		result;	result = c * f;	PG_RETURN_CASH(result);}/* flt8_mul_cash() * Multiply float8 by cash. */Datumflt8_mul_cash(PG_FUNCTION_ARGS){	float8		f = PG_GETARG_FLOAT8(0);	Cash		c = PG_GETARG_CASH(1);	Cash		result;	result = f * c;	PG_RETURN_CASH(result);}/* 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 */Datumcash_div_flt8(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	float8		f = PG_GETARG_FLOAT8(1);	Cash		result;	if (f == 0.0)		ereport(ERROR,				(errcode(ERRCODE_DIVISION_BY_ZERO),				 errmsg("division by zero")));	result = rint(c / f);	PG_RETURN_CASH(result);}/* cash_mul_flt4() * Multiply cash by float4. */Datumcash_mul_flt4(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	float4		f = PG_GETARG_FLOAT4(1);	Cash		result;	result = c * f;	PG_RETURN_CASH(result);}/* flt4_mul_cash() * Multiply float4 by cash. */Datumflt4_mul_cash(PG_FUNCTION_ARGS){	float4		f = PG_GETARG_FLOAT4(0);	Cash		c = PG_GETARG_CASH(1);	Cash		result;	result = f * c;	PG_RETURN_CASH(result);}/* 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 */Datumcash_div_flt4(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	float4		f = PG_GETARG_FLOAT4(1);	Cash		result;	if (f == 0.0)		ereport(ERROR,				(errcode(ERRCODE_DIVISION_BY_ZERO),				 errmsg("division by zero")));	result = rint(c / f);	PG_RETURN_CASH(result);}/* cash_mul_int4() * Multiply cash by int4. */Datumcash_mul_int4(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	int32		i = PG_GETARG_INT32(1);	Cash		result;	result = c * i;	PG_RETURN_CASH(result);}/* int4_mul_cash() * Multiply int4 by cash. */Datumint4_mul_cash(PG_FUNCTION_ARGS){	int32		i = PG_GETARG_INT32(0);	Cash		c = PG_GETARG_CASH(1);	Cash		result;	result = i * c;	PG_RETURN_CASH(result);}/* 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 */Datumcash_div_int4(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	int32		i = PG_GETARG_INT32(1);	Cash		result;	if (i == 0)		ereport(ERROR,				(errcode(ERRCODE_DIVISION_BY_ZERO),				 errmsg("division by zero")));	result = rint(c / i);	PG_RETURN_CASH(result);}/* cash_mul_int2() * Multiply cash by int2. */Datumcash_mul_int2(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	int16		s = PG_GETARG_INT16(1);	Cash		result;	result = c * s;	PG_RETURN_CASH(result);}/* int2_mul_cash() * Multiply int2 by cash. */Datumint2_mul_cash(PG_FUNCTION_ARGS){	int16		s = PG_GETARG_INT16(0);	Cash		c = PG_GETARG_CASH(1);	Cash		result;	result = s * c;	PG_RETURN_CASH(result);}/* 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 */Datumcash_div_int2(PG_FUNCTION_ARGS){	Cash		c = PG_GETARG_CASH(0);	int16		s = PG_GETARG_INT16(1);	Cash		result;	if (s == 0)		ereport(ERROR,				(errcode(ERRCODE_DIVISION_BY_ZERO),				 errmsg("division by zero")));	result = rint(c / s);	PG_RETURN_CASH(result);}/* cashlarger() * Return larger of two cash values. */Datumcashlarger(PG_FUNCTION_ARGS){	Cash		c1 = PG_GETARG_CASH(0);	Cash		c2 = PG_GETARG_CASH(1);	Cash		result;	result = (c1 > c2) ? c1 : c2;	PG_RETURN_CASH(result);}/* cashsmaller() * Return smaller of two cash values. */Datumcashsmaller(PG_FUNCTION_ARGS){	Cash		c1 = PG_GETARG_CASH(0);	Cash		c2 = PG_GETARG_CASH(1);	Cash		result;	result = (c1 < c2) ? c1 : c2;	PG_RETURN_CASH(result);}/* cash_words() * This converts a int4 as well but to a representation using words * Obviously way North American centric - sorry */Datumcash_words(PG_FUNCTION_ARGS){	Cash		value = PG_GETARG_CASH(0);	unsigned int val;	char		buf[256];	char	   *p = buf;	Cash		m0;	Cash		m1;	Cash		m2;	Cash		m3;	text	   *result;	/* work with positive numbers */	if (value < 0)	{		value = -value;		strcpy(buf, "minus ");		p += 6;	}	else		buf[0] = '\0';	/* Now treat as unsigned, to avoid trouble at INT_MIN */	val = (unsigned int) value;	m0 = val % 100;				/* cents */	m1 = (val / 100) % 1000;	/* hundreds */	m2 = (val / 100000) % 1000; /* thousands */	m3 = val / 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, (val / 100) == 1 ? " dollar and " : " dollars and ");	strcat(buf, num_word(m0));	strcat(buf, m0 == 1 ? " cent" : " cents");	/* capitalize output */	buf[0] = toupper((unsigned char) buf[0]);	/* make a text type for output */	result = (text *) palloc(strlen(buf) + VARHDRSZ);	VARATT_SIZEP(result) = strlen(buf) + VARHDRSZ;	memcpy(VARDATA(result), buf, strlen(buf));	PG_RETURN_TEXT_P(result);}/************************************************************************* * 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", "forty", "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 + -