Link to home
Start Free TrialLog in
Avatar of cybermilky
cybermilky

asked on

How to convert a string into a floating point value?

I have a array of string myArrayStr[1] which contains this value - 4,999.
How can I convert it into a floating point value?
How to declare a data type of Float in Delphi?
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

var
  I:     Integer;
  R:    Real;        // type of Float
  D:    Double;    // type of Float
  E:    Extended; // type of Float
  S:   string;
begin
  S := - 4,999;
//  First way is using procedure Val();
//  Val(S, R, I);  // if I=0 the conversion is successful
//  Val(S, D, I);
  Val(S, E, I);
//  Second way is using function StrToFloat();
//  R := StrToFloat(S);
//  D := StrToFloat(S);
  E := StrToFloat(S);
end;

emil
A real type defines a set of numbers that can be represented with floating-point notation. The table below gives the ranges and storage formats for the fundamental real types.

Fundamental real types
Type      Range                                                         Significant digits    Size in bytes
-----------------------------------------------------------------------------------------------------------
Real48      2.9 x 10^-39 .. 1.7 x 10^38                                        11-12           6
Single       1.5 x 10^-45 .. 3.4 x 10^38                                          7-8             4
Double      5.0 x 10^-324 .. 1.7 x 10^308                                     15-16           8
Extended  3.6 x 10^-4951 .. 1.1 x 10^4932                                  19-20          10
Comp       -2^63+1 .. 2^63 -1                                                     19-20           8
Currency  -922337203685477.5808.. 922337203685477.5807         19-20           8
>  S := - 4,999;
to be
  S := '-4,999';

example:

function  Str_To_Float(S: string; var E: Extended): Bool;
var
  I:     Integer;
  Ok:  Bool;
begin
  E := 0;
  Ok := False;
  try
    Val(S, E, I);
    Ok := (I=0);
  finally
    Result := Ok;
  end;
end;

procedure Button1Click(Sender: TObject);
var
  B:     Boolean;
  E:     Extended;
  F:     Extended;
begin
  if Str_To_Float('-4.999', B) then
  begin
    F := 2 * PI * E;
// ......................
  end;
end;
procedure Button1Click(Sender: TObject);
var
  E:     Extended;
  F:     Extended;
begin
  if Str_To_Float('-4.999', E) then
  begin
    F := 2 * PI * E;
// ......................
  end;
end;
Avatar of gwalkeriq
gwalkeriq

You also need to remove the non numeric stuff from the string commas, dollar signs, etc. that people type into strings cause the standard conversion routines to fail.

Your rules may not match this exactly, but you should get the idea. I did not testing the code, just typed it in.

function StrVal(s: string): double;
var
  num: string;
  n: integer;
begin
  for n := 1 to length(s) do
  if s[n] in ['0'..'9', '-', '+', '.'] then
     num := num + s[n];

  val(num, result, n);
end;

Code above does not handle exponential notation (1.5e3 = 1500)
it treats 3x5 as 35
it treats 2+3 as 2
hopefully you get the idea.

You'll need to decide the exact rules you need for convering strings to numbers.


ASKER CERTIFIED SOLUTION
Avatar of Delphian
Delphian

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