How to do math with decimals in Polarion

Description
adbarads
Posts: 5
Joined: Mon Nov 13, 2017 7:47 pm

How to do math with decimals in Polarion

Postby adbarads » Tue Dec 19, 2017 3:46 am

Hi,

I am creating a Wiki report in Polarion using the Velocity Templating Language.
While doing some basic scripting, I am running into this issue:
#set($verify_passed = (1/3) * (2/3))

This makes $verify_passed = 0
I am trying to find ways to deal with the decimals. It is treating everything as an integer. Any clues on this?

ALMighty
Posts: 77
Joined: Wed Apr 27, 2016 10:34 am
Location: Austria

Re: How to do math with decimals in Polarion

Postby ALMighty » Tue Jan 02, 2018 2:07 pm

Hi adbarads!

It is a bit clumsy to get Double values because you need the Mathtool for it from the very start of your calculation. In former versions you needed to import it as an extension by placing the extension in the extensions folder of your Polarion installation on your server (your Sysadmin) should know how this works) but in the current versions it should be in the core as far as I can recall, at least in my version, 2016 SR3, it works fine.
The following sample wiki page displays your example along with a faulty example at the beginning and useful links in the comments:

Code: Select all

## example of a calculation
#set($verify_passed = (1/3) * (2/3))

## print the faulty result
$verify_passed

## Does not work too, the mathTool is used wrongfully here (prints 0.0 because it reneders a faulty result, but the real result of the calculation before would be 0.2222222222222222):
$mathTool.toDouble($verify_passed)


### SOLUTION: WE NEED TO USE THE MATH TOOL FOR THE BASE CLAULCATIONS TOO FOR GETTING A DOUBLE RESULT ###
### The math tool can simply be invoked by using the variable $mathTool
### Hint 1: The extension "Wiki scripting" tools (http://extensions.polarion.com/extensions/83-wiki-scripting-tools) for Polarion should not be necessary anymore beyond Polarion version 2013
### Hint 2: The reference of the mathTool methods can be found here: https://velocity.apache.org/tools/1.4/generic/MathTool.html
### Hint 3: Check also the javadoc linked in the URL above for the full method reference: https://velocity.apache.org/tools/1.4/javadoc/org/apache/velocity/tools/generic/MathTool.html

## Thats is how ((1/3) * (2/3)) is calculated correctly in a Wiki page:
$mathTool.mul( $mathTool.div(1,3) , $mathTool.div(2,3) )
<br />
## it can be assigned to a varibale too and then printed:
#set($verify_passed = $mathTool.mul( $mathTool.div(1,3) , $mathTool.div(2,3) ) )
<h1>$verify_passed</h1>


Feel free to copy this code and trying it out in a wiki page. :D
---------------------------------------------------------------------------------------------------
Playing a good game is never lame. :D
Image


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: Harou33 and 2 guests