zee
New Member
Posts: 6
|
Post by zee on Sept 7, 2018 5:01:52 GMT
This is what I get: total = 1235 need total = 1,235
How to insert commas.
|
|
|
Post by B+ on Sept 7, 2018 12:48:03 GMT
Dang! USING() doesn't work???
|
|
|
Post by Rod on Sept 7, 2018 13:41:11 GMT
Few countries actually use this type of number presentation. That's why its not native. But you can roll your own function.
print addcomma$(0.99999) print addcomma$(999999.12) print addcomma$(99999999.99999) print addcomma$(-999999) print addcomma$(-99999.22) print addcomma$(-0.99999)
end function addcomma$(v) if v<0 then s$="-" : v=abs(v) '-sign ? i=int(v) 'absolute integer part d=v-i 'decimal part if d<>0 then d$=mid$(str$(d),2) 'remove leading 0 i$=str$(i) l=len(i$) for n=l to 1 step-1 c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma$=s$+o$+d$ end function
|
|
|
Post by B+ on Sept 7, 2018 16:39:52 GMT
I was working on my own version commafie$() and Rod posted his version before I finished. I see I forgot about negative numbers and it still fails numbers with e notation but I don't think Rod's is working correctly for e notation though it appears to comma the integer portion correctly the part after 0 seems to miss a 0 in following test: 'commafie function.txt by B+ 2018-09-07
'create commifie$(number) function since USING(frmt$, number) does not work for comma's ??
print "PRINT USING(*-###,###.00*, -10000.11) > "; PRINT USING("-###,###.00", 10000.11);" Yikes!" print print "Commafie$(-10000.11) > ";commafie$(-10000.11)
print : print "Commafie$(some random integers) >" for i = 1 to 10 'test commafie function 'create big number scan r = 1' reset r t = int(rnd(0) * 15) + 1 ' t = 1 to 15 for j = 1 to t scan r2 = int(rnd(0) * 9) + 1 r = r * r2 next if rnd(0) < .5 then r = -1 * r print r, , , ,commafie$(r) next
print : print "Test extreme number -1234567890.0987654321 with commafie$() is " print commafie$(-1234567890.0987654321); " fails from e notation."
print : print "Test extreme number -1234567890.0987654321 with addcomma$() is " print addcomma$(-1234567890.0987654321); " fails ?? from e notation."
print : print "End of test of commafie$() function."
function commafie$(number) if number < 0 then s$ = "-" : number = number * -1 'oops forgot about negative numbers n$ = str$(number) dot = instr(n$, ".") if dot then b$ = mid$(n$, dot) : n$ = mid$(n$, 1, dot - 1) while len(n$) > 3 b$ = "," + right$(n$, 3) + b$ : n$ = left$(n$, len(n$) - 3) wend if len(n$) then b$ = n$ + b$ commafie$ = s$ + b$ end function
'this works better? not sure if extreme number also fails, missing 0 after decimal function addcomma$(v) if v<0 then s$="-" : v=abs(v) '-sign ? i=int(v) 'absolute integer part d=v-i 'decimal part if d<>0 then d$=mid$(str$(d),2) 'remove leading 0 i$=str$(i) l=len(i$) for n=l to 1 step-1 c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma$=s$+o$+d$ end function
|
|
zee
New Member
Posts: 6
|
Post by zee on Sept 8, 2018 7:15:08 GMT
Rod your program worked great except the following error log:
Error log timestamp Saturday 09/08/18 03:01:42 PM
Runtime error: OS Error: The system cannot find the file specified.
FileError(Exception)>>defaultAction FileError(Exception)>>activateHandler: <anUndefinedObject> FileError(Exception)>>handle FileError(Exception)>>signal FileError class>>signal: <'OS Error: The system...'> hostErrorCode: <2> HostFileSystemError>>defaultAction HostFileSystemError(Exception)>>activateHandler: <anUndefinedObject> HostFileSystemError(Exception)>>handle HostFileSystemError(Exception)>>signal HostFileSystemError class>>signalWith: <2> FileHandle class>>osError: <2> FileHandle>>osError: <2> FileHandle(Object)>>osError FileHandle>>readInto: <' ...'> atPage: <0> pageSize: <4096> FileHandle>>readInto: <' ...'> atPosition: <0> File>>readBuffer: <' ...'> atPosition: <1> FileStream>>setLimits FileStream class>>on: <aFile> File>>readStream BasicSourceWindow>>openFileNamed: <'D:\programs\commas.b...'> BasicEditorModel>>openFileNamed: <'D:\programs\commas.b...'> BasicSourceWindow>>openThisFile: <'D:\programs\commas.b...'> Message>>perform MenuWindow>>performMenuItem: <522> BasicSourceTopPane(ApplicationWindow)>>performMenuItem: <522> Message>>perform NotificationManager>>empty NotificationManager>>runPendingEvents NotificationManager>>runEventLoop Message>>perform Message>>evaluate Process>>safelyEvaluate: <aMessage> Process>>evaluate: <aMessage>
Thanks.
|
|
|
Post by Rod on Sept 8, 2018 14:00:25 GMT
You have commas.bas stored in D:\programs\commas.bas If you open the Just BASIC IDE and navigate to the file D:\programs\commas.bas does it load into the editor using the OPEN menu? Does it look like my code and does it run?
If so are you trying to run it via a shortcut or by clicking on the .bas file itself? How are you trying to start the commas.bas program? You would need file association set for .bas for a .bas to open and run correctly if clicked on.
|
|
|
Post by Rod on Sept 8, 2018 14:21:45 GMT
I am not sure you would want to convert scientific notation numbers for display. But since Just BASIC handles the conversion it would seem my function does handle scientific notation. Whether correctly or not a mathematician would need to comment as I am not one of those.
print 1.23e5 print str$(1.23e5) print val(str$(1.23e5)) print -1.23e-5 print str$(-1.23e-5) print val(str$(-1.23e-5)) print print addcomma$(0.99999) print addcomma$(999999.12) print addcomma$(99999999.99999) print addcomma$(-999999) print addcomma$(-99999.22) print addcomma$(-0.99999) print addcomma$(1.23e5) print addcomma$(1.23e-5) print addcomma$(-1.23e5) print addcomma$(-1.23e-5) end
function addcomma$(v) if v<0 then s$="-" : v=abs(v) '-sign ? i=int(v) 'absolute integer part d=v-i 'decimal part if d<>0 then d$=mid$(str$(d),2) 'remove leading 0 i$=str$(i) l=len(i$) for n=l to 1 step-1 c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma$=s$+o$+d$ end function
|
|
|
Post by B+ on Sept 9, 2018 13:12:10 GMT
Here is an example of the type of number I am complaining about, anything with 0's to the right of the decimal (when numbers get turned over to e notation, though we never asked and rarely desire it ): print addcomma$(1.0123456789) print addcomma$(1.00123456789) print addcomma$(1.000123456789) print addcomma$(1.0000123456789) print addcomma$(1.00000123456789)
end
function addcomma$(v) if v<0 then s$="-" : v=abs(v) '-sign ? i=int(v) 'absolute integer part d=v-i 'decimal part if d<>0 then d$=mid$(str$(d),2) 'remove leading 0 i$=str$(i) l=len(i$) for n=l to 1 step-1 c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma$=s$+o$+d$ end function
I don't think you have to be a mathematician to see that it is off.
|
|
|
Post by Rod on Sept 9, 2018 14:13:15 GMT
I see the problem now. Solution might be to keep the decimal in tact and just format the integer. Needs more thinking time. Or, reverse the scientific notation before we start.
|
|
|
Post by B+ on Sept 9, 2018 16:22:14 GMT
I see the problem now. Solution might be to keep the decimal in tact and just format the integer. Needs more thinking time. Or, reverse the scientific notation before we start. Yes, it is a problem worth some time thinking. That often unexpected e notation pops up at inconvenient times and has ruined many a "perfect" code routine with number and not just in this Basic.
|
|
|
Post by Rod on Sept 10, 2018 8:31:38 GMT
The frustration is in not being able to measure the length of the incoming decimal number. Just BASIC truncates number to 8 digits for display str$() for example truncates. You can keep the precision with using() but it brings with it the usual floating point trailing trash/rounding. It probably needs a good rounding routine. So if we accept that it rounds to 8 digits the function sort of works!
print 1.23e5 print str$(1.23e5) print val(str$(1.23e5)) print -1.23e-5 print str$(-1.23e-5) print val(str$(-1.23e-5)) 'this is the problem, beyond 8 digits Liberty truncates on str$() etc print 1.0123456789 print str$(1.0123456789) print val(str$(1.0123456789))
print print addcomma$(1.0123456789) print addcomma$(0.99999) print addcomma$(999999.12) print addcomma$(99999999.99999) print addcomma$(-999999) print addcomma$(-99999.22) print addcomma$(-0.99999) print addcomma$(1.23e5) print addcomma$(1.23e-5) print addcomma$(-1.23e5) print addcomma$(-1.23e-5) print addcomma$(1.0123456789) print addcomma$(1.00123456789) print addcomma$(1.000123456789) print addcomma$(1.0000123456789) print addcomma$(1.00000123456789) end
function addcomma$(v) if v<0 then s$="-" '-sign ? v=abs(v) 'abs() v$=using("############.########",v) 'prevent truncation i$=trim$(mid$(v$,1,instr(v$,".")-1)) 'integer d$=mid$(v$,instr(v$,".")+1) 'decimal e=len(d$) while mid$(d$,e,1)="0" 'attempt to remove trailing trash e=e-1 'this needs better rounding technique wend d$=mid$(d$,1,e) if d$<>"" then d$="."+d$ 'add . to decimal l=len(i$) for n=l to 1 step-1 'add , to integer c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma$=s$+o$+d$ end function
|
|
|
Post by B+ on Sept 11, 2018 6:47:11 GMT
Nice Rod! Don't have to stop with 8 places, here is attempt for 16, 14 or 15, might do better.
'addcomma2$() mod of Rod's addcomma$(), bplus 2018-09-11
print "problem number 1.01234567890123 printed ";1.01234567890123 print "problem number str$(1.01234567890123): ";str$(1.01234567890123) print "problem number val(str$(1.01234567890123)): ";val(str$(1.01234567890123)) print "problem number addcomma$(1.01234567890123): ";addcomma2$(1.01234567890123) print print compare$(1.0123456789) print compare$(0.99999) print compare$(999999.12) print compare$(99999999.99999) print compare$(-999999) print compare$(-99999.22) print compare$(-0.99999) print compare$(1.23e5) print compare$(1.23e-5) print compare$(-1.23e5) print compare$(-1.23e-5) print print "starting with number 987654321.0123456789" print "we drop the left digit and add a 0 to right of decimal." print " they all really end at ...6789." print space$(4);compare$(987654321.0123456789) print space$(6);compare$(87654321.00123456789) print space$(7);compare$(7654321.000123456789) print space$(8);compare$(654321.0000123456789) print space$(9);compare$(54321.00000123456789) print space$(10);compare$(4321.00000123456789) print space$(11);compare$(321.000000123456789) print space$(12);compare$(21.0000000123456789) print space$(6);compare$(1.0000000123456789) print space$(3);compare$(.00000000123456789) print space$(2);compare$(.0000000000123456789) print space$(2);compare$(.00000000000123456789) end
'addcomma2$ bplus adds a little mod to Rod's code, perhaps 20 places is a bit much! ' 14, 15, 16 will likely be better. function addcomma2$(v) if v<0 then s$="-" '-sign ? v=abs(v) 'abs()
'bplus mod p = 0 'find number digits left of decimal while v > 10 ^ p p = p + 1 wend useThis$ = stringx$("#", p + 1);".";stringx$("#", 16-p-1) v$=using(useThis$,v) 'prevent truncation
i$=trim$(mid$(v$,1,instr(v$,".")-1)) 'integer d$=mid$(v$,instr(v$,".")+1) 'decimal e=len(d$) while mid$(d$,e,1)="0" 'attempt to remove trailing trash e=e-1 'this needs better rounding technique wend d$=mid$(d$,1,e) if d$<>"" then d$="."+d$ 'add . to decimal l=len(i$) for n=l to 1 step-1 'add , to integer c=c+1 if c=4 then c=1 : o$=","+o$ o$=mid$(i$,n,1)+o$ next addcomma2$=s$+o$+d$ end function
function stringx$(char$, amt) for i = 1 to amt r$ = r$ + char$ next stringx$ = r$ end function
function compare$(number) t$ = addcomma2$(number) compare$ = number;" >> ";t$ end function
|
|
|
Post by Rod on Sept 11, 2018 9:22:47 GMT
Cool, neat way to find the number of decimal digits and so set using() template correctly.
|
|
|
Post by tsh73 on Sept 11, 2018 12:51:26 GMT
|
|
|
Post by B+ on Sept 11, 2018 13:37:56 GMT
Hi tsh73, The more the merrier! Dang, another resource is closing are you guys making arrangements to preserve the wealth of info accumulated over the years? The goal in that link seems to keep numbers in a controlled space. Mine was to prevent e from coming up when presenting numbers with commas and at the same time preserve as much of the actual digits as possible. Perhaps just extending the example in the link would do that too. Come to think, the last post was controlling length to 16 plus decimal.
|
|