<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > Examples and Other Useful Things > Data Conversion > Division of a Numerical Value to Digits |
Sometimes, when forming requests to a device, it is necessary to divide a number to be sent to digits for next processing (for example, for forming a number in the BCD format from them). The next function is destined to solve such tasks.
Example of function
function UnpachNumber(Num)
local MasNum={};
repeat
local Val=Num*0.1;--divide by 10
Num,Rem=math.modf(Val); --get integer and fractional parts
local NewRem=math.modf(Rem*10); --multiply the fractional part by 10, and get an integer part of the result
table.insert(MasNum,NewRem); --write to the table
until Num==0 --exit the cycle if the integer part equals 0
return MasNum;
end;
Example of function invocation
local k={};
k=UnpachNumber(10);
--the table k will contain two elements, the numbers 1 and 0.