Link to home
Start Free TrialLog in
Avatar of MrError
MrError

asked on

question for kretzschmar...

Hello,

   I see you have alot of points so I thought I would ask you a question. I throw a value into a string something like 10.06 . I know it is an integer but I want to hold the value in a string. With this value in the string I want to take the last two character 06 and move them into another variable. This is probably very easy to do but I am not finding the answer. What do you think?

MrError
Avatar of MrError
MrError

ASKER

Edited text of question.
Two ways:
 - if you use integer instead of string
  var2 := var1 mod 100;
 - if you use string still
  var2 := Copy(var1, 4, 2);

By the way 10.06 is not integer...
Avatar of kretzschmar
hi,

i would do something like this

  if pos('.',Edit1.Text) > 0 then
    edit2.text := copy(Edit1.text,pos('.',Edit1.Text)+1,MaxLongInt);

this would take all after the point to the second var(edit2). if you want only 2 didits, then replace maxlongint with 2

meikl
good answer kretzschmar, I was going to bed before writing my  code..

But one thing...
for optimization

Index := Pos('.', Edit1.Text);
if Index > 0 then
  Edit2.Text := Copy(Edit1.Text, Index + 1, MaxLongInt);

this will generate less code... Because using Pos func two times causes two calls to that func...
yup denizcan,

thats true, this optimization will solve some nanoseconds ;-)
(well it depends how often its called,
therefore you're correct)

meikl ;-)
Avatar of MrError

ASKER

denizcan & kretzschmar,

   Thank you for both your ideas I will try using them soon. Uhhh oh yeah 10.06 is a float not an integer.
   denizcan what type is the index?  is it a string, integer?
Index := Pos('.', Edit1.Text);

MrError
hi  mrerror,

var
  index : Integer;

the function pos geives you the position of the given substring
('.' in this case) in a String (second parameter) as integer. if the
substring is not in the string the pos-function returns 0.

to solve processortime denizcan used the variable index to
store the position by the calling the pos-function once, and then
compare and action by usage of this var. cost less time than call
the pos-function twice.

well the difference is minimal,
but by exzessive usage, the better solution

meikl ;-)
Avatar of MrError

ASKER

kretzschmar,

 Ok this looks good. One more question how can I grab the first two digits? Since I am splitting this string up I will need both the values before the '.' and the ones after the '.' .

  Index := Pos('.', Edit1.Text);
  if Index > 0 then
      x := strtoint(Copy(Edit1.Text, Index + 1, MaxLongInt));

  Instead if a plus do I use - 1?

MrError
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MrError

ASKER

meikl,
 
   Man you are good. Thanks for your quick reply/solution to my problem. I think you could make a living at this programming thing.

MrError