manually setting datatypes

  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Joined: Dec 30, 2003
  • Posts: 223
  • Status: Offline

Post December 30th, 2003, 4:36 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 30th, 2003, 4:36 am

  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post December 30th, 2003, 3:40 pm

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.
Free Programming Resources
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post December 30th, 2003, 4:15 pm

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
Free Programming Resources
  • Inito
  • Graduate
  • Graduate
  • User avatar
  • Joined: Dec 30, 2003
  • Posts: 223
  • Status: Offline

Post December 31st, 2003, 3:21 am

alright, thanks dude.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 166 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.