ini return only integer when type is numeric

ini return only integer when type is numeric

Postby hua » Fri Aug 03, 2007 7:27 am

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  Expand view  RUN
[Balance]
BF_COL_K=6300
BF_COL_L=2473.50


2. The ini is then read in the following way
Code: Select all  Expand view  RUN
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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Postby StefanHaupt » Fri Aug 03, 2007 7:39 am

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().
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Postby hua » Fri Aug 03, 2007 9:43 am

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  Expand view  RUN
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  Expand view  RUN
     .
     .
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
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 37 guests