ID:172390
 
When I saw Goku72's proc for it(or atleast I assumed that's what it did at a glance :p) I figured there was a better way, so I started working on one. I finally figured it out(it was tougher than I thought it would be :\) and it seemed to work until I put higher numbers in(100,000,000,000 to be exact). Here is what I have:

proc
AddCommas(Num as num)
var/Tnum=num2text(round(Num),100000)
var/l=length(Tnum)
var/mod=1
world<<"[Tnum]=[l]"
for(var/i=3,l>i,i+=3)
var/first="[copytext(Tnum,1,length("[Tnum]")-(i-mod))]"
var/second="[copytext(Tnum,length("[Tnum]")-(i-mod))]"
Tnum="[first],[second]"
mod--
return Tnum

mob/verb/Add_Comma(n as num)
world<<"[AddCommas(n)]"


The output when starting at 1 and adding a 0 to the end everytime goes like this:

1=1
1
10=2
10
100=3
100
1000=4
1,000
10000=5
10,000
100000=6
100,000
1000000=7
1,000,000
10000000=8
10,000,000
100000000=9
100,000,000
1000000000=10
1,000,000,000
10000000000=11
10,000,000,000
99999997952=11 *Notice the input here was 100000000000.
99,999,997,952

Can anyone see what's wrong with this?
Floating point numbers have a limited precision, and they go by base 2 which means if you go high enough up by powers of 10, it's not 1.0×10n but really 5.0n×2n, and eventually the number runs out of bits to express the 5.0n part.

Solution: Don't use numbers that high.

Lummox JR
In response to Lummox JR
Thanks. =)
YMIHere wrote:
When I saw Goku72's proc for it(or atleast I assumed that's what it did at a glance :p) I figured there was a better way, so I started working on one. I finally figured it out(it was tougher than I thought it would be :\) and it seemed to work until I put higher numbers in(100,000,000,000 to be exact). Here is what I have:

> proc
> AddCommas(Num as num)
> var/Tnum=num2text(round(Num),100000)
> var/l=length(Tnum)
> var/mod=1
> world<<"[Tnum]=[l]"
> for(var/i=3,l>i,i+=3)
> var/first="[copytext(Tnum,1,length("[Tnum]")-(i-mod))]"
> var/second="[copytext(Tnum,length("[Tnum]")-(i-mod))]"
> Tnum="[first],[second]"
> mod--
> return Tnum
>
> mob/verb/Add_Comma(n as num)
> world<<"[AddCommas(n)]"
>


Actually, I believe the number begins to go funky above 10 billion. Anyhow, ya my proc's to add commas, I thought of doing it like what you did, but if you speak to anyone who knows me - they'll tell you I hate using loops when they're not necessary. >.>
In response to Goku72
Goku72 wrote:
Actually, I believe the number begins to go funky above 10 billion. Anyhow, ya my proc's to add commas, I thought of doing it like what you did, but if you speak to anyone who knows me - they'll tell you I hate using loops when they're not necessary. >.>

I like loops when there's endless possibilities(or near endless), but as Lummox just taught me, there isn't in this situation. =)