mit dieser Funktion lassen sich Integerwerte für die Scriptausgabe formatieren. Es erfolgt keine Rundung.
Int2Str(val, f_dist[, dpow [,unit [,fix] ] ] )
val: zu formatierender Integerwert
f_dist: Entfernungsflag [0,1]; [1] setzt das Ergebnis auf "inf", wenn (val<=0) oder (val>=65535)
dpow: Zehnerpotenz zur Formatierung=10^dpow; z.B. [2] val=*100, [-2] val=*0,01
unit: Einheit oder ""
fix: Anzahl der festen Nachkommastellen
Syntax: [ Download ] [ Verstecken ]
Benutze Lua Syntax Highlighting
function Int2Str(val, f_dist, dpow, unit, fix)
local sval=tostring(math.abs(val))
local res=tostring(val)
if (f_dist==1 and (val<=0 or val>=65535)) then res="inf" end
if (sval~="nil" and res~="inf" and dpow~=nil) then
local slen=string.len(sval)
local scount=math.abs(dpow)
local least=""
if (scount==dpow) then
res=sval..string.rep("0",scount)
if (fix~=nil) then least=string.rep("0",fix) end
else
if (slen<=scount) then
sval=string.rep("0",scount-slen+1)..sval
slen=string.len(sval)
end
res=string.sub(sval,1,slen-scount)
least=string.sub(sval,slen-scount+1)
local l_l=string.len(least)
if (fix==nil) then
while (l_l>0 and string.sub(least,l_l)=="0") do
least=string.sub(least,1,l_l-1)
l_l=string.len(least)
end
else
if (l_l>math.abs(fix)) then least=string.sub(least,1,fix) end
if (l_l<math.abs(fix)) then least=least..string.rep("0",fix-l_l) end
end
end
if (string.len(least)>0) then res=res.."."..least end
if (val<0) then res="-"..res end
end
if (unit~=nil and res~="inf" and string.len(unit)>0) then res=res.." "..unit end
return res
end
local sval=tostring(math.abs(val))
local res=tostring(val)
if (f_dist==1 and (val<=0 or val>=65535)) then res="inf" end
if (sval~="nil" and res~="inf" and dpow~=nil) then
local slen=string.len(sval)
local scount=math.abs(dpow)
local least=""
if (scount==dpow) then
res=sval..string.rep("0",scount)
if (fix~=nil) then least=string.rep("0",fix) end
else
if (slen<=scount) then
sval=string.rep("0",scount-slen+1)..sval
slen=string.len(sval)
end
res=string.sub(sval,1,slen-scount)
least=string.sub(sval,slen-scount+1)
local l_l=string.len(least)
if (fix==nil) then
while (l_l>0 and string.sub(least,l_l)=="0") do
least=string.sub(least,1,l_l-1)
l_l=string.len(least)
end
else
if (l_l>math.abs(fix)) then least=string.sub(least,1,fix) end
if (l_l<math.abs(fix)) then least=least..string.rep("0",fix-l_l) end
end
end
if (string.len(least)>0) then res=res.."."..least end
if (val<0) then res="-"..res end
end
if (unit~=nil and res~="inf" and string.len(unit)>0) then res=res.." "..unit end
return res
end
Erstellt in 0.013 Sekunden, mit GeSHi 1.0.8.9
Beispiel:
Eine Motiventfernung von 1234 mm soll in Meter mit 2 Nachkommastellen angezeigt werden
Int2Str(1234,1,-3,"m",2) => 1.23 m
[EDIT]
Verarbeitung negativer Werte korrigiert.
[/EDIT]
Gruß rudi.