How to convert decimal to whole number??
by Donnie Hutson Jr · in Torque 3D Professional · 03/18/2014 (11:27 pm) · 12 replies
I have this in script;
it returns a decimal number, example 90.3245.... How can I convert %tempdist either up or down in script to just a whole number??? thanks in advance.....
//Determine the distance from the player to the target %tempdist = vectorDist(%playerposition, %botpos);
it returns a decimal number, example 90.3245.... How can I convert %tempdist either up or down in script to just a whole number??? thanks in advance.....
About the author
Electrical Engineer by trade, Computer geek by night. Still learning and loving every minute.
#2
03/19/2014 (9:38 am)
The old Tribes 2 RPG Mod had a mathematical function for rounding numbers using standard math. Here's the implementation of it:function round(%n) {
if(%n < 0) {
%t = -1;
%n = -%n;
}
else if(%n >= 0) {
%t = 1;
}
%f = mfloor(%n);
%a = %n - %f;
if(%a < 0.5) {
%b = 0;
}
else if(%a >= 0.5) {
%b = 1;
}
return mfloor((%f + %b) * %t);
}
#3
03/19/2014 (9:44 am)
Great thanks guys.... works great!! One quick question, is this value in yards, meters, feet???
#4
03/19/2014 (9:47 am)
Maybe we should add an mround() to the engine. We're already making two calls to mfloor() there so it would have to be faster to just make one call....
#5
03/19/2014 (12:31 pm)
I thought there was an mRound in the engine. Although i think i saw a post of it being broken. It was either working like Floor or Ceil I cant remember which.
#7
And it is there - for some reason Torsion didn't want to act like it was though - (in engine/math/mConsoleFunctions.cpp):
EDIT
Looks like they all work, too:
03/19/2014 (2:54 pm)
Meters? What on Earth for? It's just a number rounding function, not a world unit conversion tool....And it is there - for some reason Torsion didn't want to act like it was though - (in engine/math/mConsoleFunctions.cpp):
DefineConsoleFunction( mRound, S32, ( F32 v ),,
"Round v to the nearest integer.n"
"@param v Number to convert to integer."
"@returns Number converted to integer."
"@ingroup Math" )
{
return (S32)mFloor( v + 0.5f );
}Guess that's one way to do it....EDIT
Looks like they all work, too:
%i = 1.6;
echo(" -- floor " @ %i @ " : " @ mFloor(%i));
echo(" -- ceiling " @ %i @ " : " @ mCeil(%i));
echo(" -- round " @ %i @ " : " @ mRound(%i));producedQuote:in the console.
-- floor 1.6 : 1
-- ceiling 1.6 : 2
-- round 1.6 : 2
#8
03/19/2014 (4:32 pm)
very cool... thanks guys!!
#9
03/19/2014 (4:48 pm)
@Richard, I took it as what is the torque unit or whatever the coordinate system is based upon considering his above code had vectors, not actually dealing with the mRound :P
#10
03/19/2014 (5:09 pm)
jeffs right, I only meant torque unit, sorry for the confusion. mRound works great and nice to know thanks everyone....
#11
03/19/2014 (6:27 pm)
See? I told you guys I never pay attention in class....
#12
Compressed Pc Game
www.intelgamesz.blogspot.com
04/22/2014 (10:57 pm)
I thought there was an mRound in the engine.Compressed Pc Game
www.intelgamesz.blogspot.com
Torque Owner Richard Ranft
Roostertail Games
This always rounds up, so 90.00001 is 91.