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
MrErrorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MrErrorAuthor Commented:
Edited text of question.
0
denizcanCommented:
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...
0
kretzschmarCommented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

denizcanCommented:
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...
0
kretzschmarCommented:
yup denizcan,

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

meikl ;-)
0
MrErrorAuthor Commented:
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
0
kretzschmarCommented:
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 ;-)
0
MrErrorAuthor Commented:
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
0
kretzschmarCommented:
just pasted from your other q

hi MrError,

a function which so both

//Function for split strings
function SplitIt(Splitter : String;
                 Var ToSplit : String) : String;
Var P : Integer;
begin
  P := Pos(Splitter, ToSplit);
  If P = 0 then P := Length(ToSplit)+1;
  Result := Copy(ToSplit,1,p-1);
  ToSplit := Copy(ToSplit,p+length(splitter),MaxLongInt);
end;

example

mayAfter := MyHoleString;
myBefore := SplitIt('.',MyAfter);

MyAfter holds now only this after the point

meikl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrErrorAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.