Hello all,
Is there a way in I7 to display large numbers with commas separating the thousands; 12,000 FEET, 13,500 FEET, &c.?
Many thanks,
J. J. Guest
Hello all,
Is there a way in I7 to display large numbers with commas separating the thousands; 12,000 FEET, 13,500 FEET, &c.?
Many thanks,
J. J. Guest
Not built in. You’d have to write it.
Suspected as much. Thanks Zarf.
I’m in the process of porting my TADS 2 game, To Hell in a Hamper, over to I7, which, considering that I’ve written much larger and more complex I7 games, is proving much more of a headache than I imagined. (For my reasons, see this thread [url]https://intfiction.org/t/npapi-no-longer-supported-in-chrome-firefox/8636/1])
In the original TADS 2 code, the altitude of the balloon was expressed as a three-digit number called startroom.height, which started at 120.
I simply converted the number into a string using the function cvtstr( num ), and then used the substr( str, ofs, len ) to find the first, second and third letters of the string:
heightVerb: darkVerb
verb = 'height'
sdesc = "height"
action(Actor) =
{
"The balloon is ";
say(substr(cvtstr(startroom.height),1,2));
",";
say(substr(cvtstr(startroom.height),3,1));
"00 feet above the ground. ";
say(startroom.height);
}
;
I can’t find any way to do this in I7. What’s more, and I may be looking in the wrong places, but I can’t find any way to do this in I6 either.
An alternative approach would be to divide the altitude by 100 (call this X)
Then round down X to the nearest whole number to give the first digit (call this Y)
Then subtract the whole number Y from X to give the remainder (Z), and multiply Z by 100 to give the last two digits.
The problem with this approach is that I can’t find any way in I7 to return a whole number.
Any help / ideas would be greatly appreciated!
I7 handles numbers as integers so they’re automatically truncated.
You can add the commas with this recursive code, which works for all positive numbers:
To decide which text is (N - a number) with commas:
let R be "[the remainder after dividing N by 1000]";
if N is greater than 1000:
let R be "[N divided by 1000 with commas],[R]";
decide on R.
The description of the balloon is "The balloon is [height of balloon with commas] feet above the ground."
If you know that the number is always smaller than one million, a regex solution might be more elegant (YMMV):
To decide which text is (N - a number) with commas:
let R be "[N]";
replace the regular expression "(\d+)(\d{3})$" in R with "\1,\2";
decide on R.
Juhana, you’re a marvel. Thank you!
Here’s a slightly more verbose version of the string manipulation solution:
[code]To say (N - a number) with commas:
let T be “[N]”;
let len be the number of characters in T;
while len > 3:
let D be character number (len - 3) in T;
replace character number (len - 3) in T with “[D],”;
now len is len - 3;
say T.
When play begins: say “[432 with commas]; [1002 with commas]; [12323 with commas]; [12345678 with commas]; [1234567890 with commas].”
Lab is a room. [necessary to make it compile]
[/code]
The key is replacing a character with the character followed by the comma–this effectively inserts the comma after the character. I put in a lot of temporary variable names that aren’t strictly necessary to make the code slightly less difficult to read, though maybe this obscures the fact that D and character number (len - 3) in T are the same thing. Also note that let T be “[N]” casts the numerical representation of N into text, which is what I’m guessing cvtstr does in TADS 3.
Here’s a version with no string manipulation at all, which makes it the fast solution:
To say (N - number) with commas:
if N < 0:
say "-";
now N is 0 - N;
if N >= 1000:
let M be N / 1000;
say M with commas;
say ",";
now N is N - ( M * 1000 );
if N < 100:
say "0";
if N < 10:
say "0";
say N.
A belated thank you for all these answers! I’ve been away for a few days.
I had no idea you could even write things like “let len be the number of characters in T”…
Very enlightening!
– Jason