Trim Number: Difference between revisions
Jump to navigation
Jump to search
Verisimilar (talk | contribs) mNo edit summary |
m (Taustin moved page trim Number to Trim Number without leaving a redirect) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
===Usage=== | ===Usage=== | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
trimNumber(number) | trimNumber(number) | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
trimNumber(number, length) | trimNumber(number, length) | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
trimNumber(number, length, direction) | trimNumber(number, length, direction) | ||
</ | </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''' | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[defineFunction("trimNumber", "trimNumber@this", 1)] | [defineFunction("trimNumber", "trimNumber@this", 1)] | ||
</source> | </source> | ||
<hr><br> | <hr><br> | ||
<hr>'''trimNumber''' | <hr>'''trimNumber''' | ||
< | <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] | ||
</ | </syntaxhighlight> | ||
<hr><br> | <hr><br> | ||
===Examples=== | ===Examples=== | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[r: trimNumber(1.125000)] | [r: trimNumber(1.125000)] | ||
</ | </syntaxhighlight> | ||
Returns {{code|1.125}} | Returns {{code|1.125}} | ||
< | <syntaxhighlightlang="mtmacro"> | ||
[r: trimNumber(1.125000, 2)] | [r: trimNumber(1.125000, 2)] | ||
</ | </syntaxhighlight> | ||
Returns {{code|1.13}} | Returns {{code|1.13}} | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[r: trimNumber(1.124000, 2)] | [r: trimNumber(1.124000, 2)] | ||
</ | </syntaxhighlight> | ||
Returns {{code|1.12}} | Returns {{code|1.12}} | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[r: trimNumber(1.123000, 2, "up")] | [r: trimNumber(1.123000, 2, "up")] | ||
</ | </syntaxhighlight> | ||
Returns {{code|1.13}} | Returns {{code|1.13}} | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[r: trimNumber(1.128000, 2, "down")] | [r: trimNumber(1.128000, 2, "down")] | ||
</ | </syntaxhighlight> | ||
Returns {{code|1.12}} | Returns {{code|1.12}} | ||
[[Category:Cookbook]] | [[Category:Cookbook]] |
Latest revision as of 23:59, 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 to10
.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