From: Sandro Santilli Date: Fri, 15 Oct 2004 09:41:01 +0000 (+0000) Subject: Added a trailing zeros trimmer X-Git-Tag: pgis_1_0_0RC1~280 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=57bf5a303f9b667b15d2f7efc4935cf9e9b7c59a;p=postgis Added a trailing zeros trimmer git-svn-id: http://svn.osgeo.org/postgis/trunk@1005 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/lwgeom/liblwgeom.c b/lwgeom/liblwgeom.c index db91d7894..84436ddf4 100644 --- a/lwgeom/liblwgeom.c +++ b/lwgeom/liblwgeom.c @@ -136,3 +136,36 @@ lwfree(void *mem) { return lwfree_var(mem); } + +/* + * Removes trailing zeros and dot for a %f formatted number. + * Modifies input. + */ +void +trim_trailing_zeros(char *str) +{ + char *ptr, *totrim=NULL; + int len; + int i; + + //lwnotice("input: %s", str); + + ptr = strchr(str, '.'); + if ( ! ptr ) return; // no dot, no decimal digits + + //lwnotice("ptr: %s", ptr); + + len = strlen(ptr); + for (i=len-1; i; i--) + { + if ( ptr[i] != '0' ) break; + totrim=&ptr[i]; + } + if ( totrim ) + { + if ( ptr == totrim-1 ) *ptr = '\0'; + else *totrim = '\0'; + } + + //lwnotice("output: %s", str); +} diff --git a/lwgeom/liblwgeom.h b/lwgeom/liblwgeom.h index 1ead401c3..2e0fe8311 100644 --- a/lwgeom/liblwgeom.h +++ b/lwgeom/liblwgeom.h @@ -1020,4 +1020,7 @@ extern void *lwalloc(size_t size); extern void *lwrealloc(void *mem, size_t size); extern void lwfree(void *mem); +/* Utilities */ +extern void trim_trailing_zeros(char *num); + #endif // !defined _LIBLWGEOM_H