Skip to content

Instantly share code, notes, and snippets.

@McTwist
Created August 16, 2017 00:10
Show Gist options
  • Save McTwist/adcbaaef5b4d692e0e93a2ccf4276aae to your computer and use it in GitHub Desktop.
Save McTwist/adcbaaef5b4d692e0e93a2ccf4276aae to your computer and use it in GitHub Desktop.
Correctly converts a number in TorqueScript to a string
// Converts a number to a string correctly
function num2str(%num)
{
if (mAbs(%num) < 1000000)
return "" @ %num;
if (%num < 0)
{
%neg = true;
%num = -%num;
}
%decimals = %num - mFloor(%num);
%decilen = strlen(%decimals);
%million = mFloor(%num / 1000000);
%num = %num % 1000000;
%len = 6 - strlen(%num);
for (%i = 0; %i < %len; %i++)
%num = 0 @ %num;
return (%neg ? "-" : "") @ %million @ %num @ (%decilen > 1 ? getSubStr(%decimals, 1, %decilen) : "");
}
@McTwist
Copy link
Author

McTwist commented Jan 4, 2018

I found out that on line 13 it will get precision error in a matter of 0.0001. Currently unsure of how to solve that, as it will generate complete wrong result for some part. Also, it will for calculated numbers still generate invalid data, despite still being correct. This solution is not the best one and wont work with most numbers above one million.

TorqueScript sucks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment