Log In  

Cart #strdsum-0 | 2023-02-02 | Code ▽ | Embed ▽ | No License
5

Feature Overview

STRDSUM() sums two numerable strings.

  • The number of digits it can handle exceeds the 32-bit limit. (Probably as long as the system will allow.)
  • Match the string length to the longer of the two arguments.
  • Negative values and values after the decimal point cannot be handled.(Insert a period in the output result.)
  • This function consumes 63 Token.

This code may cause errors in versions prior to 0.2.5c.

?strdsum('32768','32768') -- 65536

?strdsum('500','000500') -- 001000

?strdsum('900109','123456') -- 1023565
P#125166 2023-02-02 12:58 ( Edited 2023-05-13 11:59)

A subtractive version has been added.
Only the result is supported in the minus display.

function strdsub(a,b)
    local s,l,d,m='',max(#a,#b),'',''
    if a < b then
        a,b,m=b,a,'-'
    end
    for i=1,l do
        local v=(a[-i] or 0)-(b[-i] or 0)-#d
        d=v<0 and '1' or '' 
        s=v%10 ..s
    end
    while s[1]=='0' do
        s=sub(s,2)
    end
    return m..s
end
P#134584 2023-09-19 13:25 ( Edited 2023-09-19 13:27)

[Please log in to post a comment]