Convert a string representing a real to a number

This script extracts the integer portion and decimal portion of a string value.

You can find the corresponding script file under Script_Examples, sub-directory of scripts.

 

'Function dec_val extracts the integer portion and decimal portion of a string value

 

'Parameters are the string which is the real value, decimal_sep$ is the decimal separator (comma, decimal point, etc.)

' p_integer is the integer portion, p_decim is the decimal portion

'(both are integers). This function returns 1 if the separator was found, 0 if not

 

 

function dec_val(string$,decimal_sep$,p_integer,p_decim)

local pos, a$, b$, res

 

pos=instr(string$,decimal_sep$)

if pos=0 then

res=0

p_decim=0

p_integer=val(string$)

else

a$=left$(string$,pos-1)

b$=right$(string$,len(string$)-pos)

p_integer=val(a$)

p_decim=val(b$)

'deletes the rightmost 0

while (p_decim mod 10) = 0

p_decim=p_decim/10

wend

res=1

endif

 

dec_val=res

endfunction

 

 

'Example

 

number$="-1256111.44555000"

separator$="."

result=0

 

result=dec_val(number$,separator$,integ,decimal)

if result=1 then

msg$="Real Number : "+number$ +"\n\Integer part : "+str$(integ)+"\n\Decimal part : "+str$(decimal)

else

msg$="Number "+number$+", is not decimal\n\Conversion result : "+str$(intg)

endif

msgbox(msg$,48,"Decimal conversion")