How can I round numbers in a viewport?

75 views
Skip to first unread message

kbarkan

unread,
May 23, 2019, 4:09:30 PM5/23/19
to Scroll Viewport Developers
I need to do some work with percentages. This requires that I be able to round some numbers. If I were using a macro in Confluence directly, I could access several helpers directly to do it, like $generalUtil. But in Scroll Viewport, I can't access $generalUtil.

So, how can I round numbers when I'm coding for Viewport?

kbarkan

unread,
May 28, 2019, 10:58:33 AM5/28/19
to Scroll Viewport Developers
I could really use some help with this.

Roman Serazhiev (K15t)

unread,
May 28, 2019, 11:09:48 AM5/28/19
to Scroll Viewport Developers
Hi,

as Apache Velocity 1.6 is used for Viewport themes, I would suggest to search on how to round numbers with Velocity.

These links could help:

Roman.

kbarkan

unread,
May 28, 2019, 12:35:13 PM5/28/19
to Scroll Viewport Developers
Unfortunately, the classes referenced in both of those solutions, as well as every other solution I've found, are not accessible when rendering a vm file in Scroll Viewport. Anything that talks about rounding using MathTool, generalUtils.convertToInteger or NumberTool. None of which I can access.

Each of these solutions also expects you to be able to add these tools to the context, which I do not know how to do. Due to limitations and restrictions in our environment, I have to be able to do everything with an out-of-the-box instance of Confluence Server and of Scroll Viewport. If there is a syntax for adding those classes to a vm using Scroll Viewport, I would be happy to try it out. But even things that are supposed to be available to me according to Confluence's documentation are not in Viewport, like generalUtils.

If you can see a way to get it going, a two-line template should be enough to show it works:
## line 1: include whatever is needed to add rounding to the page, if necessary
## line 2: the command to round the number 1.5 

For instance, if this worked, it would be enough to show it in action:
$context.put("Math", Math.class);
#Math.round(1.5)

Thanks for looking at it.

kbarkan

unread,
May 29, 2019, 9:51:50 AM5/29/19
to Scroll Viewport Developers
I've worked it out. I found some other solutions that didn't work, but after reading several, I was able to piece it together.

1. You need a variable to use to access the .*class.forName() method. The variable has no bearing on it, other than a way to get to the method.
2. Put the name of the class you want to import
3. Add .newInstance() at the end.

So for getting MathTool into scope I did this:
#set($dummyVar = "")
#set($MathTool = $dummyVar.class.forName("org.apache.velocity.tools.generic.MathTool").newInstance())
$MathTool.round(1.5)

I don't know if that's a good idea or a bad idea. But it worked. I only found one answer anywhere that included the .newInstance() method, which was the key to get things working.



On Thursday, May 23, 2019 at 4:09:30 PM UTC-4, kbarkan wrote:

Steffen Burzlaff (K15t)

unread,
May 29, 2019, 11:30:58 AM5/29/19
to Scroll Viewport Developers
Hi,

sorry for the late response. The way you figured out ist the only way I also could think off. Velocity 1.6 which is used in the environment doesn't provide a math function.
You could also develop a pluggable placeholder to have that more robust in your code and create an actual $math function, which I would also recommend to do.

Best,
Steffen

Jens Rutschmann (K15t)

unread,
Jun 27, 2019, 7:11:08 AM6/27/19
to scroll-vi...@googlegroups.com
Hi,

in Scroll Viewport 2.12.2 we've added a new $math placeholder to the Velocity context based on the java.lang.Math class.
See https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html for supported operations.


Cheers,
Jens

kbarkan

unread,
Sep 27, 2019, 4:31:41 PM9/27/19
to Scroll Viewport Developers
FYI, it seems that around 2.14.2, the inclusion of the $math placeholder somehow made this stop working:
#set($dummyVar = "")
#set($MathTool = $dummyVar.class.forName("org.apache.velocity.tools.generic.MathTool").newInstance())
$MathTool.round(1.5)

Luckily I could just switch it to:
$math.round(1.5)

kbarkan

unread,
Sep 27, 2019, 4:41:16 PM9/27/19
to Scroll Viewport Developers
Also, $math doesn't appear in the latest docs.

Jens Rutschmann (K15t)

unread,
Sep 30, 2019, 4:23:10 AM9/30/19
to scroll-vi...@googlegroups.com
Hi,

it might have been a caching issue before, but in any case it is documented here:
https://help.k15t.com/scroll-viewport/latest/page-context-125708775.html

We don't have a dedicated page for it because we're simply reusing a standard Java class for it that is already documented publicly (same as
with $dateTime or $stringUtils). See the entry on that context overview page for a $math documentation link.


Cheers,
Jens

kbarkan

unread,
Sep 30, 2019, 9:56:20 AM9/30/19
to Scroll Viewport Developers
Thanks. It might be a search index problem. No results here: https://help.k15t.com/scroll-viewport/search?q=math&max=10
Reply all
Reply to author
Forward
0 new messages