The following code: [H: broadcast(0.1 * 10)]
produces this: 1.0
Is there a function to have Maptool round off numbers nicely, i.e. the answer should simply be: 1 without any trailing digits.
Rounding off numbers
Moderators: dorpond, trevor, Azhrei
Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior.
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior.
Re: Rounding off numbers
GETTING STARTED WITH MAPTOOLS - TUTORIALS, DOCS, VIDEOS, TOOLS, ETC
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
DISCORD (the new MT forum!)
My stuff
Excel Tools: Table and Light editors
MT Tools: Bag of Tricks: Tools for Maptool, Dungeon Builder I, Dungeon Builder II,onMouseOverEvent.
Frameworks: Dark Heresy, Rogue Trader, Deathwatch, Black Crusade, Only War, SET Card Game, RoboRally
Wiki: Debugging Tutorial, Speed Up Your Macros, Working With Two CODE Levels, Shortcut Keys, Avoiding Stack Overflow, READ THIS
Re: Rounding off numbers
If you use a decimal or if a decimal is calculation along the way, it will always display in decimal. But as wolph said, those functions will remove the decimal as well as Wiki: strformat() options.nirkedar wrote:The following code: [H: broadcast(0.1 * 10)]
produces this: 1.0
Is there a function to have Maptool round off numbers nicely, i.e. the answer should simply be: 1 without any trailing digits.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Rounding off numbers
I know about ceiling, floor & round.
However they will round numbers off even when there is a legitimate fraction.
I need a function that will only truncate the trailing zeros.
However they will round numbers off even when there is a legitimate fraction.
I need a function that will only truncate the trailing zeros.
Re: Rounding off numbers
Alrighty, had to manually write my own macro called TRUNCATE to remove trailing zeros. In case anyone is interested, here it is:
So if I ran:
[R: TRUNCATE(1.020)]
[R: TRUNCATE(1.00)]
The results will be
1.02
1
Code: Select all
[H: Number = ARG(0)]
[H: Fraction = Number - floor(Number)]
[H: Result = round(Number)]
[H, IF(Fraction>0), CODE: {
[ size = length(Fraction)]
[ lastDigit= substring(Fraction, size-1, size)]
[ WHILE(lastDigit==0), CODE: {
[ size = size - 1]
[ lastDigit = substring(Fraction, size-1, size)]
}]
[ Fraction = substring(Fraction, 0, size)]
[ Result = Result + Fraction]
}]
[H: macro.return = Result]
[R: TRUNCATE(1.020)]
[R: TRUNCATE(1.00)]
The results will be
1.02
1
Re: Rounding off numbers
You can probably use some regex and Wiki: replace() for a single line to remove trailing 0s.
Now if you don't give it a fraction then it will remove 0s of a whole number so you may want to add a check for that.
Code: Select all
<!-- remove trailing 0s -->
[r: num = round(1.0200,4)]<br>
[r: newNum = replace(num,"[.]*0+\$","")]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools