Created
August 16, 2017 00:10
-
-
Save McTwist/adcbaaef5b4d692e0e93a2ccf4276aae to your computer and use it in GitHub Desktop.
Correctly converts a number in TorqueScript to a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) : ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.