Trim Number: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
mNo edit summary
m (Taustin moved page trim Number to Trim Number without leaving a redirect)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:


===Usage===
===Usage===
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
trimNumber(number)
trimNumber(number)
</source>
</syntaxhighlight>
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
trimNumber(number, length)
trimNumber(number, length)
</source>
</syntaxhighlight>
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
trimNumber(number, length, direction)
trimNumber(number, length, direction)
</source>
</syntaxhighlight>
'''Parameters:'''
'''Parameters:'''
{{param|number|The number to be trimmed.}}
{{param|number|The number to be trimmed.}}
Line 20: Line 20:
Place the following macros on the same library token(or on different library tokens if you're know what you're doing and what to change).
Place the following macros on the same library token(or on different library tokens if you're know what you're doing and what to change).
<hr>'''onCampaignLoad'''
<hr>'''onCampaignLoad'''
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[defineFunction("trimNumber", "trimNumber@this", 1)]
[defineFunction("trimNumber", "trimNumber@this", 1)]
</source>
</source>
<hr><br>
<hr><br>
<hr>'''trimNumber'''
<hr>'''trimNumber'''
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[assert(argCount() != 0, "trimNumber() requires at least one parameter.")]
[assert(argCount() != 0, "trimNumber() requires at least one parameter.")]
[assert(argCount() <= 3, "trimNumber() accepts a maximum of three parameters.")]
[assert(argCount() <= 3, "trimNumber() accepts a maximum of three parameters.")]
Line 50: Line 50:
]
]
[macro.return = NumberToTrim]
[macro.return = NumberToTrim]
</source>
</syntaxhighlight>
<hr><br>
<hr><br>


===Examples===
===Examples===
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[r: trimNumber(1.125000)]
[r: trimNumber(1.125000)]
</source>
</syntaxhighlight>
Returns {{code|1.125}}
Returns {{code|1.125}}


<source lang="mtmacro">
<syntaxhighlightlang="mtmacro">
[r: trimNumber(1.125000, 2)]
[r: trimNumber(1.125000, 2)]
</source>
</syntaxhighlight>
Returns {{code|1.13}}
Returns {{code|1.13}}


<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[r: trimNumber(1.124000, 2)]
[r: trimNumber(1.124000, 2)]
</source>
</syntaxhighlight>
Returns {{code|1.12}}
Returns {{code|1.12}}


<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[r: trimNumber(1.123000, 2, "up")]
[r: trimNumber(1.123000, 2, "up")]
</source>
</syntaxhighlight>
Returns {{code|1.13}}
Returns {{code|1.13}}


<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[r: trimNumber(1.128000, 2, "down")]
[r: trimNumber(1.128000, 2, "down")]
</source>
</syntaxhighlight>
Returns {{code|1.12}}
Returns {{code|1.12}}


[[Category:Cookbook]]
[[Category:Cookbook]]

Latest revision as of 17:57, 3 May 2023

Requires MapTool 1.3b56
The following is a user defined function that allows you to trim any trailing zeros from a floating point number(a number with decimal places). It also allows you to truncate the decimal places to a certain length and round any truncated decimal places in a certain direction.

Usage

trimNumber(number)
trimNumber(number, length)
trimNumber(number, length, direction)

Parameters:

  • number - The number to be trimmed.
  • length - How many decimal places the trimmed number should retain, defaults to 10.
  • direction - A string containing the direction that any truncated decimals placed should be rounded it. Accepts "up", "u", "down", and "d", defaults to rounding to the nearest.

Macros

Place the following macros on the same library token(or on different library tokens if you're know what you're doing and what to change).


onCampaignLoad

[defineFunction("trimNumber", "trimNumber@this", 1)]
</source>
<hr><br>
<hr>'''trimNumber'''
<syntaxhighlight lang="mtmacro">
[assert(argCount() != 0, "trimNumber() requires at least one parameter.")]
[assert(argCount() <= 3, "trimNumber() accepts a maximum of three parameters.")]
[NumberToTrim = arg(0)]
<!-- Set truncation depth -->
[if(argCount() >= 2), code:
{
    [TruncationDepth = power(10, arg(1))]
};{
    [TruncationDepth = power(10, 10)]
}]
<!-- Set rounding method -->
[if(argCount() == 3), code:
{
    [RoundingMethod = substring(arg(2), 0 , 1)]
};{
    [RoundingMethod = 0]
}]
<!-- Perform trim -->
[switch(RoundingMethod):
    case "u": NumberToTrim = ceiling(NumberToTrim*TruncationDepth)/TruncationDepth;
    case "d": NumberToTrim = floor(NumberToTrim*TruncationDepth)/TruncationDepth;
    default: NumberToTrim = round(NumberToTrim*TruncationDepth)/TruncationDepth
]
[macro.return = NumberToTrim]


Examples

[r: trimNumber(1.125000)]

Returns 1.125

<syntaxhighlightlang="mtmacro"> [r: trimNumber(1.125000, 2)] </syntaxhighlight> Returns 1.13

[r: trimNumber(1.124000, 2)]

Returns 1.12

[r: trimNumber(1.123000, 2, "up")]

Returns 1.13

[r: trimNumber(1.128000, 2, "down")]

Returns 1.12