Difference between revisions of "Common:Convert Variables to Numbers"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
Line 1: Line 1:
<noinclude>__NOINDEX__</noinclude>When you want to do calculations on a currency field in Velocity, you need to create a number from the currency strings delivered by the platform. That string has the form <tt>$24.95</tt>. The following code converts it into a number:
<noinclude>__NOINDEX__</noinclude>When you want to do calculations on a currency field in Velocity, you need to create a number from the currency strings delivered by the platform. That string has the form <tt>$24.95</tt>. The following code converts it to a number that can be used in calculations:
:<syntaxhighlight lang="html4strict" enclose="div">
:<syntaxhighlight lang="html4strict" enclose="div">
#set($n = 0.00)
#set($n = 0.00)

Latest revision as of 00:35, 16 November 2013

When you want to do calculations on a currency field in Velocity, you need to create a number from the currency strings delivered by the platform. That string has the form $24.95. The following code converts it to a number that can be used in calculations:

#set($n = 0.00)
#set($s = $YourObject.currency_field.substring(2) )
#set($n = $n.parseDouble($s)

where:

  • $n = 0.00 creates an instance of the double-precision float class (Double)
  • $YourObject.currency_field.substring(2) removes the first two characters from the currency string. (The $ sign and the space that follows it.)
  • parseDouble($s) converts the resulting string into a double-precision float--a number that can be used in calculations.