AiGeneric Insert Decimal Function?
I am trying to write a function that takes any number and inserts it into the video buffer that the AiGeneric television text driver creates.
The place I am stuck at is extracting the digits in the number to a string. Once I can do that it is a simple matter of iterating through each digit and setting the corresponding buffer position equal to the digit.
So what is a fast way to extract the decimal digits in a number, preferably in Spin?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Not the fish.
sites.google.com/site/bitwinproject/
The place I am stuck at is extracting the digits in the number to a string. Once I can do that it is a simple matter of iterating through each digit and setting the corresponding buffer position equal to the digit.
So what is a fast way to extract the decimal digits in a number, preferably in Spin?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Not the fish.
sites.google.com/site/bitwinproject/

Comments
Leon
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Amateur radio callsign: G1HSM
Suzuki SV1000S motorcycle
PUB fdec(value, len, dp) | p, fnumbuf[noparse][[/noparse]4] '' Print a formatted signed decimal number '' The original dec function inexplicably doesn't do fixed '' length, even though the hex and bin functions do, making '' it nearly worthless for practical use. This version also '' inserts an optional decimal point. A negative dp specifies '' dead zeroes, e.g. dp=-3 returns 000 instead of 0. '' '' First we prep the buffer with any leading zero and '' decimals that might be called for, then write the number '' down backward skipping over the decimal and skipping '' over any remaining zeroes for the minus sign. bytefill(@fnumbuf," ",14) byte[noparse][[/noparse]@fnumbuf+14] := 0 p := @fnumbuf + 13 if dp > 0 repeat dp byte[noparse][[/noparse]p] := "0" p-- byte[noparse][[/noparse]p] := "." p-- byte[noparse][[/noparse]p] := "0" p-- elseif dp < 0 repeat -dp byte[noparse][[/noparse]p] := "0" p-- else byte[noparse][[/noparse]p] := "0" p-- rdec(value, @fnumbuf+13) str(@fnumbuf + 14 - len) PRI rdec(value, atloc) | s { This is works like dec but writes the number to memory down from the starting atloc. It will not write over a decimal point; the string should be pre-loaded with any necessary leading zeroes and decimals. There are no controls so be sure the buffer is large enough to handle a signed, decimalled 9-digit number. } s := 0 if value < 0 -value s := "-" repeat while value <> 0 'skip over decimal if byte[noparse][[/noparse]atloc] == "." atloc -- byte[noparse][[/noparse]atloc] := value // 10 + "0" atloc -- value /= 10 if s 'skip over any non space before writing sign repeat while byte[noparse][[/noparse]atloc] <> " " atloc -- byte[noparse][[/noparse]atloc] := sPost Edited (localroger) : 9/28/2009 12:41:23 AM GMT
PRI extractDigits(num, str) | i, power, ndig, digit bytefill(@str, 0, 2) power := 0 repeat while num/toPower(10, power)>9 power += 1 ndig := power+1 repeat i from 0 to ndig-1 digit := num/toPower(10, power) data[i] := digit num -= toPower(10, power)*digit power -= 1 PRI toPower(val, pwr) | i, num num := val if pwr==1 return val if pwr==0 return 1 if pwr>1 repeat i from 0 to pwr-2 num *= val return num [/i]The digits end up in the data array. I am only using 1 or 2 digit numbers, so this works well.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Not the fish.
sites.google.com/site/bitwinproject/