?
Solved

How to convert a string to double

Posted on 1998-07-13
10
Medium Priority
?
360 Views
Last Modified: 2010-05-18
i have a problem in converting a string from a PChar type to a double type? may you suggest a method to perform this easy task?
0
Comment
Question by:smile_tbw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Expert Comment

by:bengore
ID: 1357838
Try this:
var d:double;
    p:PChar;
begin
  d:=1.09;
  p:=PChar(@d);
end;

If you want convert a string to an UNICODE string (array of double byte char), then use
WideCharToMultiByte() and MultiByteToWideChar().

The WideCharToMultiByte function maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.

The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357839
Hi smile_tbw,

 ?

Is this what you mean :

var TextPChar : PChar;
    Number : Double;
begin
 TextPChar := '5000,4';
 Number := StrToFloat(TextPChar);
end;

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357840
Well, I was too late again... but bengore, isn't the answer supposed to be the other way?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smile_tbw
ID: 1357841
I think the answer propose by ZifNab is the answer i want but not by bengore. BUT how to give the point to ZifNab instead of bengore?? Please help!!

In addition, my string is ended with a CR($D) and LF(£A). How to avoid it because it can't convert by using StrToFloat. Thanks.
0
 

Author Comment

by:smile_tbw
ID: 1357842
I'm sorry, bengore. you answer seem to be reversed to my question.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 240 total points
ID: 1357843
Hi smile_tbw. Just remove the two last chars from the string, use copy or delete to do it :

var TextPChar : PChar;
           Number : Double;
       begin
        TextPChar := '5000,4'+char($D)+char($A);
        showmessage('with CR and LF :'+TextPChar);
        showmessage('without : '+Copy(TextPChar,1,Length(TextPChar)-2));
        Number := StrToFloat(Copy(TextPChar,1,Length(TextPChar)-2));
        ShowMessage('GoodLuck!');
       end;

Regards, Zif.
Have a nice day.
0
 

Author Comment

by:smile_tbw
ID: 1357844
hi Zif,

In addition my string is ended with a CR($D) and LF($A). How to avoid it because it can't convert by using StrToFloat

Thnaks.

0
 

Author Comment

by:smile_tbw
ID: 1357845
hey, Zif,
sorry about the point was accidentally reduced to 6. Actually i want to increase it to 60. BUT.. .. . .. Sorry again. Hope you don't mind.

Thank you very much
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357846
Hi smile_tbw,

look at answer I gave you... I remove the two last chars (namely CR and LF) from the string before I use StrToFloat.

this is the function : Copy(TextPChar,1,Length(TextPChar)-2)

Ok,I just copy a part of the total string (all the char, without the last 2), but maybe it's also possible with other functions, e.g. Delete ...

The beauty of the copy is that you can still use the total string for futher processing.

Just copy my example and you got it.

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357847
Smile_tbw,

nope don't mind, only don't know if you can reduce the points.

->hi Zif,

       In addition my string is ended with a CR($D) and LF($A). How to avoid it because it can't convert by using StrToFloat

       Thnaks.

I think you found it in the answer I gave you :-), otherwise you wouldn't accept my answer so soon.

ZiF.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question