HP OpenVMS Systems

ask the wizard
Content starts here

setlocale printf formatting bug?

» close window

The Question is:

 
I have several reports written in C that use the setlocale() function to setup
 formated printing of numbers (99,999,999.99).  The problem that I am having is
 that when a number is negative and is 3,6 or 9 digits in length is will print
 in the following fo
rmat -,999.99 or -,999,999.00.  Is there any way to prevent the comma from
 printing after the sign or to use a different locale.
 
Compaq C version - v6.0-001
 
locale used - EN_US_ISO8859-1.LOCALE;1
 


The Answer is :

 
  This appears to be a bug within the printf family of functions, a
  bug which adversely effects formatting negative numbers by the %d
  and %i directives when locale-specific thousands' grouping character
  is requested (by specifying apostrophe flag character on the format
  directive).
 
  As the %f directive does not seem to be affected by the bug, the
  use %'f direcive instead of %'d or %'i would be the most obvious
  workaround, prior to the availability of a fix.
 
  The bug will be fixed in a future release of the Compaq C Run-Time
  Library on OpenVMS.
 
  Thank you for reporting this.
 
 
$ cc/ver
Compaq C V6.2-005 on OpenVMS Alpha V7.2-1H1
$ run x.exe
%f directive: -999.99
%d directive: -,999
%i directive: -,999
 
%f directive: -999,999.99
%d directive: -,999,999
%i directive: -,999,999
 
%f directive: -999,999,999.99
%d directive: -,999,999,999
%i directive: -,999,999,999
 
 
x.c
---
#include [stdio.h]
#include [locale.h]
 
main()
 
  if ( !setlocale(LC_ALL, "en_US.ISO8859-1") )
    perror("setlocale");
 
  /* three digits */
  printf("%%f directive: %'.2f\n", -999.99);
  printf("%%d directive: %'d\n",   -999);
  printf("%%i directive: %'i\n\n", -999);
 
  /* six digits */
  printf("%%f directive: %'.2f\n", -999999.99);
  printf("%%d directive: %'d\n",   -999999);
  printf("%%i directive: %'i\n\n", -999999);
 
  /* nine digits */
  printf("%%f directive: %'.2f\n", -999999999.99);
  printf("%%d directive: %'d\n",   -999999999);
  printf("%%i directive: %'i\n",   -999999999);
 
 

answer written or last revised on ( 2-OCT-2001 )

» close window