IBM Support

Example of ILE RPG Calling the ATOF( ) C Function and How it Behaves with Locales

Troubleshooting


Problem

This document provides an example of ILE RPG calling the ATOF( ) C function and how it behaves with locales.

Resolving The Problem

Caution: This is an example only. IBM accepts no responsibility for its correctness.


Here is an example of ILE RPG calling the ATOF ( ) function.

Example

ds                s              8a   inz('1.15')                      
                                                                       
dx                s              8f                                    
datof             pr             8f   extproc('atof')                  
dfld1                             *   Value Options(*String)            
 /free                                                                  
    x=atof(s);                                                          
 /end-free                                                              
c     x             dsply                                              
c                   seton                                        lr    

If the user profile locale is set to *none, this works and the ATOF returns the following:

DSPLY +1.150000000000000E+000

If the user profile locale is set to /QSYS.LIB/FR_CA.LOCALE , the following. is received:

DSPLY +1.000000000000000E+000

To resolve the issue that occurs because of the locale setting, you must code the program similar to the following.

The setlocale() C function is called to set the correct locale for the RPG program. Below is an RPG program that retrieves the current
locale setting, sets the new locale, calls the ATOF() function, and then restores the previous locale setting:

h bnddir('QC2LE')                                                    
d s               s              8a   inz('1.15')                    
D LC_ALL          c                   -1                              
D LOCALETYPE_POSIX...                                                
D                 c                   'POSIX'                        
D oldLocaleSave   s           1000a   varying                        
D oldLocalePtr    s               *                                  
D setlocale       pr              *   extproc('_C_PSX_setlocale')    
D  category                     10i 0 value                          
D  localetype                     *   value options(*string)          
d x               s              8f                                  
d atof            pr             8f   extproc('atof')                
d  fld1                           *   Value Options(*String)          
 /free                                                                
    // try it without any locale setting                              
    x=atof(s);                                                        
    dsply ('with cur locale = "' + %char(x) + '"');                  
    // save the current locale value                                  
    oldLocalePtr = setlocale (LC_ALL : *NULL);                        
    oldLocaleSave = %str(oldLocalePtr);                              
    // set the locale                                                
    setlocale (LC_ALL : LOCALETYPE_POSIX);                            
    // do the locale-sensitive function                              
    oldLocalePtr = setlocale (LC_ALL : *NULL);                        
    x=atof(s);                                                        
    dsply ('with new locale = "' + %char(x) + '"');                  
    // restore the previous locale                                    
    setlocale (LC_ALL :  oldLocaleSave);                              
    // try it again with the previous locale setting
    x=atof(s);                                                        
    dsply ('with prv locale = "' + %char(x) + '"');                  
    *inlr = '1';                                                      
 /end-free                                                            

If the user profile locale is set to *none, this is the outcome:

DSPLY with cur locale = "+1.150000000000000E+000"
DSPLY with new locale = "+1.150000000000000E+000"
DSPLY with prv locale = "+1.150000000000000E+000"

If the user profile locale is set to /QSYS.LIB/FR_CA.LOCALE, this is the outcome:

DSPLY with cur locale = "+1.000000000000000E+000"
DSPLY with new locale = "+1.150000000000000E+000"
DSPLY with prv locale = "+1.000000000000000E+000"

[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"6.1.0"}]

Historical Number

553096101

Document Information

Modified date:
18 December 2019

UID

nas8N1012446