Page 1 of 1

ini return only integer when type is numeric

Posted: Fri Aug 03, 2007 7:27 am
by hua
If a numeric value such as 100.50 is stored in an ini file, reading it back will return 100 not 100.50. The decimal part is somehow excluded. The following is the full scenario

1. App.ini contents:

Code: Select all

[Balance]
BF_COL_K=6300
BF_COL_L=2473.50
2. The ini is then read in the following way

Code: Select all

function readIni()
  local oIni, aBF := array(2)
  INI oIni FILE "c:\app\data\app.ini"
      GET aBF[COL_K] SECTION "Balance" ENTRY "BF_COL_K"  OF oIni DEFAULT 0.00
      GET aBF[COL_L] SECTION "Balance" ENTRY "BF_COL_L"  OF oIni DEFAULT 0.00
  ENDINI
return nil
Instead of 6300 and 2473.50, I'll get 6300 and 2473 :shock:. How to fix this?

Am using FWH2.8 and the code was tested on an XP machine

Thank you

Posted: Fri Aug 03, 2007 7:39 am
by StefanHaupt
Hua,

the TIni-class does not support floating-point values, but you can save and read these value as a string and then convert it back to numeric with Val().

Posted: Fri Aug 03, 2007 9:43 am
by hua
Thanks for the reply Stefan :) . Initially I was surprised because I thought if TIni class was able to correctly read/set variable type such as date and logical then surely a numeric type would've been handled correctly also.

My initial workaround was to modify my readIni() to the following (saveIni() seems to be working as expected):

Code: Select all

function readIni() 
  local oIni, aBF := array(2) 
  INI oIni FILE "c:\app\data\app.ini" 
      GET aBF[COL_K] SECTION "Balance" ENTRY "BF_COL_K"  OF oIni DEFAULT ""
      GET aBF[COL_L] SECTION "Balance" ENTRY "BF_COL_L"  OF oIni DEFAULT ""
  ENDINI 
  aeval(aBF,{|x,ele| aBF[ele] := val(x)}
return nil
But later, I changed the TIni:Get() method the following way and revert back my changes to readIni().

Code: Select all

     .
     .
if Empty( ::cIniFile )
   uVar = GetProfString( cSection, cEntry, cValToChar( uDefault ) )
   if cType == "N"
      uVar := val(uVar)
   endif
else
   uVar = GetPvProfString( cSection, cEntry, cValToChar( uDefault ),;
                           ::cIniFile )
   if cType == "N"
      uVar := val(uVar)
   endif
endif
     .
     .
Seems to be working fine so far