![]() |
Telelogic DOORS (steve huntington) | ![]() |
new topic :
profile :
search :
help :
dashboard :
calendar :
home
|
||
Latest News:
|
|
Topic Title: How to get an Integer value of a string? Topic Summary: Created On: 5-Jul-2004 13:07 Status: Post and Reply |
Linear : Threading : Single : Branch |
![]() |
![]()
|
![]() |
|
Hi, Karsten.
You can try "int i = intOf("5")", for example. If you would have the real part of a number, try "int r = realOf("3,14")". regards, dirk. ------------------------- Dirk Plaschke |
|
![]() |
|
![]() |
|
EDIT: oups, didnt think intOf worked on strings, no need for the stoi/atoi function.
(sorry) It is, here's a little example (not sure if there are any math functions like power(x,y) in DXL, so this is a bit "ugly") -------------------- int stoi(string x){ int i int power=1 int result=0 //since I didnt know any mathFunctions like power in DXL, I made an ugly loop for i in 1:length x do power=power*10 //main loop, rather self-explaining for(i=0;i<length x;i++){ power = power/10 result = result + power*intOf(x[i:i]) } return result } -------------------- stoi = string-to-integer the reason that I use 2 loops that work with power is that the first loop is of reverse order of the last; you might want a few controlls that the letters really are digits in the loop. Im sure this can be written in an easier way, this was just ... on top of my head. Hope it helps Alex Edited: 5-Jul-2004 at 13:52 by Alexander Lundh |
|
![]() |
|
![]() |
|
Hi, Alex.
For the 'ugly loop', you can use "power = intOf(pow( 10.0, realOf(length(x)-1)))". dirk. ------------------------- Dirk Plaschke |
|
![]() |
|
![]() |
|
Just more info:
intOf() will throw an error if you pass it a non-numeric string. You could test the string with isValidInt() first. Some Math functions in DXL: sin cos tan exp log pow sqrt random Refer to the DXL Reference Manual (under DOORS Help menu) for more information. Thanks --------- chun.ho(at)motorola.com |
|
![]() |
|
![]() |
|
//***************
bool fStringToInt(string InString, int &OutInt) { // Convert the InString to a valid Integer. Return whether it worked. // Improper or null Strings return False and Zero OutInt = 0 if (null InString) return(false) if (!isValidInt(InString)) return(false) OutInt = intOf(InString) return(true) } // end fStringToInt() |
|
![]() |
Telelogic DOORS
» DXL Exchange
»
How to get an Integer value of a string?
|
![]() |
FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.