99 * workings can be found in the book "Software Solutions in C" by
1010 * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
1111 *
12- * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.51 2001/10/25 05:49:43 momjian Exp $
12+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.52 2002/02/19 22:19:34 tgl Exp $
1313 */
1414
1515#include "postgres.h"
@@ -287,7 +287,7 @@ cash_out(PG_FUNCTION_ARGS)
287287if (value < 0 )
288288{
289289minus = 1 ;
290- value * =-1 ;
290+ value = - value ;
291291}
292292
293293/* allow for trailing negative strings */
@@ -301,8 +301,8 @@ cash_out(PG_FUNCTION_ARGS)
301301else if (comma && count % (mon_group + 1 )== comma_position )
302302buf [count -- ]= comma ;
303303
304- buf [count -- ]= (value %10 )+ '0' ;
305- value /= 10 ;
304+ buf [count -- ]= (( unsigned int ) value %10 )+ '0' ;
305+ value = (( unsigned int ) value ) / 10 ;
306306}
307307
308308strncpy ((buf + count - strlen (csymbol )+ 1 ),csymbol ,strlen (csymbol ));
@@ -664,6 +664,7 @@ Datum
664664cash_words (PG_FUNCTION_ARGS )
665665{
666666Cash value = PG_GETARG_CASH (0 );
667+ unsignedint val ;
667668char buf [128 ];
668669char * p = buf ;
669670Cash m0 ;
@@ -682,10 +683,13 @@ cash_words(PG_FUNCTION_ARGS)
682683else
683684buf [0 ]= '\0' ;
684685
685- m0 = value %100 ;/* cents */
686- m1 = (value /100 ) %1000 ;/* hundreds */
687- m2 = (value /100000 ) %1000 ;/* thousands */
688- m3 = value /100000000 %1000 ;/* millions */
686+ /* Now treat as unsigned, to avoid trouble at INT_MIN */
687+ val = (unsignedint )value ;
688+
689+ m0 = val %100 ;/* cents */
690+ m1 = (val /100 ) %1000 ;/* hundreds */
691+ m2 = (val /100000 ) %1000 ;/* thousands */
692+ m3 = val /100000000 %1000 ;/* millions */
689693
690694if (m3 )
691695{
@@ -705,7 +709,7 @@ cash_words(PG_FUNCTION_ARGS)
705709if (!* p )
706710strcat (buf ,"zero" );
707711
708- strcat (buf , (int ) ( value /100 )== 1 ?" dollar and " :" dollars and " );
712+ strcat (buf , (val /100 )== 1 ?" dollar and " :" dollars and " );
709713strcat (buf ,num_word (m0 ));
710714strcat (buf ,m0 == 1 ?" cent" :" cents" );
711715
@@ -733,7 +737,7 @@ num_word(Cash value)
733737"zero" ,"one" ,"two" ,"three" ,"four" ,"five" ,"six" ,"seven" ,
734738"eight" ,"nine" ,"ten" ,"eleven" ,"twelve" ,"thirteen" ,"fourteen" ,
735739"fifteen" ,"sixteen" ,"seventeen" ,"eighteen" ,"nineteen" ,"twenty" ,
736- "thirty" ,"fourty " ,"fifty" ,"sixty" ,"seventy" ,"eighty" ,"ninety"
740+ "thirty" ,"forty " ,"fifty" ,"sixty" ,"seventy" ,"eighty" ,"ninety"
737741};
738742const char * * big = small + 18 ;
739743int tu = value %100 ;