mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +03:00
Add text<->float8 and text<->float4 conversion functions.
This will fix the problem reported by Jose' Soares when trying to cast a float to text.
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.33 1998/09/01 04:32:32 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.34 1998/11/17 14:36:44 thomas Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -926,6 +926,96 @@ i2tof(int16 num)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* float8_text - converts a float8 number to a text string
|
||||
*/
|
||||
text *
|
||||
float8_text(float64 num)
|
||||
{
|
||||
text *result;
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = float8out(num);
|
||||
len = (strlen(str)+VARHDRSZ);
|
||||
|
||||
result = palloc(len);
|
||||
|
||||
VARSIZE(result) = len;
|
||||
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
||||
|
||||
pfree(str);
|
||||
return result;
|
||||
} /* float8_text() */
|
||||
|
||||
|
||||
/*
|
||||
* text_float8 - converts a text string to a float8 number
|
||||
*/
|
||||
float64
|
||||
text_float8(text *string)
|
||||
{
|
||||
float64 result;
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
len = (VARSIZE(string) - VARHDRSZ);
|
||||
str = palloc(len + 1);
|
||||
memmove(str, VARDATA(string), len);
|
||||
*(str + len) = '\0';
|
||||
|
||||
result = float8in(str);
|
||||
pfree(str);
|
||||
|
||||
return result;
|
||||
} /* text_float8() */
|
||||
|
||||
|
||||
/*
|
||||
* float4_text - converts a float4 number to a text string
|
||||
*/
|
||||
text *
|
||||
float4_text(float32 num)
|
||||
{
|
||||
text *result;
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
str = float4out(num);
|
||||
len = (strlen(str)+VARHDRSZ);
|
||||
|
||||
result = palloc(len);
|
||||
|
||||
VARSIZE(result) = len;
|
||||
memmove(VARDATA(result), str, (len - VARHDRSZ));
|
||||
|
||||
pfree(str);
|
||||
return result;
|
||||
} /* float4_text() */
|
||||
|
||||
|
||||
/*
|
||||
* text_float4 - converts a text string to a float4 number
|
||||
*/
|
||||
float32
|
||||
text_float4(text *string)
|
||||
{
|
||||
float32 result;
|
||||
int len;
|
||||
char *str;
|
||||
|
||||
len = (VARSIZE(string) - VARHDRSZ);
|
||||
str = palloc(len + 1);
|
||||
memmove(str, VARDATA(string), len);
|
||||
*(str + len) = '\0';
|
||||
|
||||
result = float4in(str);
|
||||
pfree(str);
|
||||
|
||||
return result;
|
||||
} /* text_float4() */
|
||||
|
||||
|
||||
/*
|
||||
* =======================
|
||||
* RANDOM FLOAT8 OPERATORS
|
||||
|
Reference in New Issue
Block a user