velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Serge Matveenko <...@i-free.ru>
Subject Re: String to int
Date Thu, 06 Jul 2006 15:14:34 GMT
Big thanks!

But look, following works nice too:)

## converts $valueOfString to $valueOfInt and sets $valueOfInt to FALSE
if first is not an integer.

#set( $valueOfString = $valueOfString.trim() )
#set( $valueOfCharsCount = $valueOfString.length() - 1 )
#set( $valueOfVector = '0123456789' )
#set( $valueOfIsNum = true )
#set( $valueOfBase = 1 )
#set( $valueOfInt = 0 )
#foreach( $valueOfCharIterator in [$valueOfCharsCount..0] )
	#set( $valueOfChar =
$valueOfString.charAt( $valueOfCharIterator ).toString() )
	#if( $valueOfVector.contains( $valueOfChar ) )
		#set( $valueOfInt = $valueOfInt +
$valueOfVector.indexOf( $valueOfChar ) * $valueOfBase )
	#else
		#set( $valueOfIsNum = false )
	#end
	#set( $valueOfBase = $valueOfBase * 10 )
#end
#if( !$valueOfIsNum )
	#set( $valueOfInt = $valueOfIsNum )
#end

В Чтв, 06/07/2006 в 16:32 +0200, apache@recks.org пишет:

> Use the Java Integer class as a Tool.
> 
> #set( $Integer = 1 )
> #set( $myInt = $Integer.valueOf( $myNumericalString ) )
> # The valueOf(...) may take a second parameter "radix"
> # other cool method is the toHexString()
> 
> You can do the same for Long and other Number subclasses:
> #set( $Long = $Integer.longValue() )##
> ## doubleValue()
> ## floatValue()
> ## shortValue()
> ## byteValue()
> 
> Note that velocity will always return the Object form instead
> of the primitive class.
> 
> Look in the ApiDoc for more of this...
> #set( $String = "1" )
> #set( $Char = $String.charAt(0) )
> 
> Cheers,
> Christoph
> 
> Serge Matveenko wrote:
> > Hello!
> > It might be that my question has been asked many times but i couldn't
> > find the answer:(
> > 
> > I need to convert 'String' object containing representation of the
> > number to 'int' value using only Velocity features.
> > Now i have only one solution: just write simple converting macros that
> > will compare each character of the string with all 10 digits.
> > Could somebody make it better?
> > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 

-- 
Serge Matveenko
I-Free
+7 911 777 42 44
mailto:lig@i-free.ru
http://company.i-free.ru

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message