manually setting datatypes

  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

whats the use of setting a string such as "This is a string" (whatever string) as an int, or a double?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

A number of languages provide methods or functions for parsing ints and reals out of strings, but I think many would give you an error if you tried to cast a string directly to a numeric type, although I suppose it would depend on the language.

I know java doesn't allow it because strings are objects and can't be converted directly to primitive types via casting. In php if you try to cast a string to an int it will try to parse the first integer it finds out of the string (if the first character isn't a number the result will be zero). Javascript provides a parseInt method that does much the same thing.

I guess it depends on the language and what you are trying to do. If you are trying to extract a numeric type then there may be some provision for this in the language or you might create one yourself, but if the string doesn't contain a numeric type I'm not sure what the use would be other than determining that a numeric value isn't present.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

By the way, if you are thinking in terms of php then one possible use is to make sure that a non-numeric value hasn't been substituted where you don't want one - say in some input from a form to one of your scripts.

This page has an example of doing that under the "Check User Variables" section:

http://www.zend.com/zend/art/art-oertli.php
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Posts: 223

Post 3+ Months Ago

alright, thanks dude.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 91 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.