tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/1-how-do-i-convert-from-string-to-int-or-any-other-number-typeCascade CMS: Discussion 2018-10-18T20:35:56Ztag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T14:31:34Z2010-05-27T14:33:09Zhow do i convert from string to int (or any other number type)?<div><p>Velocity is a bit tricky when it comes to casting values, as the
standard Java casting operator, <code>(type)</code>, isn't
available. The classes in the <a href=
"http://velocity.apache.org/tools/devel/generic.html">Velocity
Tools project</a>, specifically <a href=
"http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/MathTool.html">
MathTool</a> can help, but MathTool isn't yet available in
Cascade's Velocity context.</p>
<p>That being said, there <em>is</em> still a way to cast a String
to an Integer, Float, etc. You simply have to create a static
Integer, Float, etc. first:<br></p>
<pre>
<code>#set( $Integer = 0 )
#set( $numberAsString = "12345" )
#set( $realNumber = $Integer.parseInt($numberAsString) )</code>
</pre>
<p>What's happening here is that <a href=
"http://velocity.apache.org/engine/devel/vtl-reference-guide.html#aset_-_Establishes_the_value_of_a_reference">
number literals in Velocity create an instance of the appropriate
class</a>. Once we have an instance of the Integer class, Velocity
<em>also allows us to call the <strong>static</strong> methods of
that class</em>, once of which is <code>parseInt(String
s)</code>.</p></div>Rosstag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T14:48:20Z2010-05-27T14:48:20Zhow do i convert from string to int (or any other number type)?<div><p>Please also feel free to monitor the issue below for
implementation progress, as we have identified this as an
improvement item.</p>
<p><a href=
"http://support.hannonhill.com/browse/CSCD-6774">http://support.hannonhill.com/browse/CSCD-6774</a></p>
<p>Thanks!</p></div>Joeltag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T16:59:38Z2010-05-27T16:59:38Zhow do i convert from string to int (or any other number type)?<div><p>hi ross,</p>
<p>i had originally tried that code but parseInt() threw an
exception (see<br>
attached screenshot). after your response i looked at it again and
i<br>
just figured out the problem. when the format self-validates, it's
not<br>
receiving $contentRoot, so it has no values for any datadef fields
and<br>
just uses null, which is invalid input for parseInt().</p>
<p>so i just needed to insert a dummy value like so:<br>
#set( $imgWidth = "472" ) #set( $imgWidth =
$imgNode.getChild("srcWidth").value ) #set( $integer = 0 ) #set(
$headWidth = 708 - $integer.parseInt($imgWidth) )</p>
<p>thanks ross!</p>
<p>chris :)</p>
<p>-----Original Message----- From: ross.williams<br>
[mailto:<a href=
"mailto:tender+dba31a394bbb9b02e5ba564795591c42aae87bf10@tenderapp.com">tender+dba31a394bbb9b02e5ba564795591c42aae87bf10@tenderapp.com</a>]
Sent: Thursday, May 27, 2010 7:34 AM</p></div>ccctag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T18:30:02Z2010-05-27T21:07:33Zhow do i convert from string to int (or any other number type)?<div><p>If you ever want to do something like this without having to
initialize the variable with a default value, you can say:<br></p>
<pre>
<code>#if( $mightBeNull )
do things that fail on null values
#end</code>
</pre>
<p>Velocity's #if statement resolves null to false.</p></div>Rosstag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T21:34:01Z2010-05-27T21:36:05Zhow do i convert from string to int (or any other number type)?<div><p>Is there a listing of which Velocity tools are available in the
Cascade Velocity context?</p></div>jkreuzigtag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T21:59:13Z2010-05-27T22:01:28Zhow do i convert from string to int (or any other number type)?<div><p>This page lists the available tools in the Technical section
under Velocity Tools:<br>
<a href=
"http://www.hannonhill.com/kb/Script-Formats/index.html">http://www.hannonhill.com/kb/Script-Formats/index.html</a></p></div>Miketag:help-archives.hannonhill.com,2010-02-09:Comment/17878172010-05-27T22:01:01Z2010-06-01T13:22:22Zhow do i convert from string to int (or any other number type)?<div><p><a href=
"http://www.hannonhill.com/kb/Script-Formats/index.html">http://www.hannonhill.com/kb/Script-Formats/index.html</a></p>
<p>Under the Technical section there is a Velocity Tools
heading.</p></div>Mike