mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-12 13:49:39 +02:00
+ Added float - string conversion examples
This commit is contained in:
parent
a14f83b1c0
commit
cdf2d7408e
@ -39,7 +39,7 @@ OBJECTS=ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12\
|
|||||||
ex48 ex49 ex50 ex51 ex52 ex53 ex54 ex55 ex56 ex57 ex58\
|
ex48 ex49 ex50 ex51 ex52 ex53 ex54 ex55 ex56 ex57 ex58\
|
||||||
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex69 ex70\
|
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex69 ex70\
|
||||||
ex71 ex72 ex73 ex74 ex75 ex76 ex77 ex78 ex79 ex80 \
|
ex71 ex72 ex73 ex74 ex75 ex76 ex77 ex78 ex79 ex80 \
|
||||||
ex81 ex82 ex83 ex84 ex85 ex86 ex87 ex88
|
ex81 ex82 ex83 ex84 ex85 ex86 ex87 ex88 ex89 ex90 ex91
|
||||||
|
|
||||||
# This might not be the same list as objects, since some of the
|
# This might not be the same list as objects, since some of the
|
||||||
# tests might be interactive.
|
# tests might be interactive.
|
||||||
@ -50,7 +50,7 @@ TOTEST=ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12\
|
|||||||
ex48 ex49 ex50 ex51 ex52 ex53 ex54 ex55 ex56 ex57 ex58\
|
ex48 ex49 ex50 ex51 ex52 ex53 ex54 ex55 ex56 ex57 ex58\
|
||||||
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex69 ex70\
|
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex69 ex70\
|
||||||
ex72 ex73 ex74 ex75 ex76 ex77 ex78 ex79 ex80 \
|
ex72 ex73 ex74 ex75 ex76 ex77 ex78 ex79 ex80 \
|
||||||
ex81 ex82 ex83 ex84 ex85 ex86 ex87 ex88
|
ex81 ex82 ex83 ex84 ex85 ex86 ex87 ex88 ex89 ex90 ex91
|
||||||
|
|
||||||
LOGFILE=$(addsuffix .log, $(TOTEST))
|
LOGFILE=$(addsuffix .log, $(TOTEST))
|
||||||
|
|
||||||
|
@ -89,3 +89,6 @@ ex86.pp contains an example of the TrimRight function.
|
|||||||
ex87.pp contains an example of the UpperCase function.
|
ex87.pp contains an example of the UpperCase function.
|
||||||
ex88.pp contains an example of the LastDelimiter function.
|
ex88.pp contains an example of the LastDelimiter function.
|
||||||
|
|
||||||
|
ex89.pp contains an example of the FormatFloat function.
|
||||||
|
ex90.pp contains an example of the StrToFloat function.
|
||||||
|
ex91.pp contains an example of the TextToFloat function.
|
||||||
|
45
docs/sysutex/ex89.pp
Normal file
45
docs/sysutex/ex89.pp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
Program Example89;
|
||||||
|
|
||||||
|
{ This program demonstrates the FormatFloat function }
|
||||||
|
|
||||||
|
Uses sysutils;
|
||||||
|
|
||||||
|
Const
|
||||||
|
NrFormat=9;
|
||||||
|
FormatStrings : Array[1..NrFormat] of string = (
|
||||||
|
'',
|
||||||
|
'0',
|
||||||
|
'0.00',
|
||||||
|
'#.##',
|
||||||
|
'#,##0.00',
|
||||||
|
'#,##0.00;(#,##0.00)',
|
||||||
|
'#,##0.00;;Zero',
|
||||||
|
'0.000E+00',
|
||||||
|
'#.###E-0');
|
||||||
|
NrValue = 5;
|
||||||
|
FormatValues : Array[1..NrValue] of Double =
|
||||||
|
(1234,-1234,0.5,0,-0.5);
|
||||||
|
|
||||||
|
Width = 12;
|
||||||
|
FWidth = 20;
|
||||||
|
|
||||||
|
Var
|
||||||
|
I,J : Integer;
|
||||||
|
S : String;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Write('Format':FWidth);
|
||||||
|
For I:=1 to NrValue do
|
||||||
|
Write(FormatValues[i]:Width:2);
|
||||||
|
Writeln;
|
||||||
|
For I:=1 to NrFormat do
|
||||||
|
begin
|
||||||
|
Write(FormatStrings[i]:FWidth);
|
||||||
|
For J:=1 to NrValue do
|
||||||
|
begin
|
||||||
|
S:=FormatFloat(FormatStrings[I],FormatValues[j]);
|
||||||
|
Write(S:Width);
|
||||||
|
end;
|
||||||
|
Writeln;
|
||||||
|
end;
|
||||||
|
End.
|
40
docs/sysutex/ex90.pp
Normal file
40
docs/sysutex/ex90.pp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
Program Example90;
|
||||||
|
|
||||||
|
{ This program demonstrates the StrToFloat function }
|
||||||
|
{$mode objfpc}
|
||||||
|
{$h+ }
|
||||||
|
|
||||||
|
Uses SysUtils;
|
||||||
|
|
||||||
|
Const
|
||||||
|
NrValues = 5;
|
||||||
|
TestStr : Array[1..NrValues] of string =
|
||||||
|
('1,1','-0,2','1,2E-4','0','1E4');
|
||||||
|
|
||||||
|
Procedure Testit;
|
||||||
|
|
||||||
|
Var
|
||||||
|
I : Integer;
|
||||||
|
E : Extended;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Writeln('Using DecimalSeparator : ',DecimalSeparator);
|
||||||
|
For I:=1 to NrValues do
|
||||||
|
begin
|
||||||
|
Writeln('Converting : ',TestStr[i]);
|
||||||
|
Try
|
||||||
|
E:=StrToFloat(TestStr[i]);
|
||||||
|
Writeln('Converted value : ',E);
|
||||||
|
except
|
||||||
|
On E : Exception do
|
||||||
|
Writeln('Exception when converting : ',E.Message);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
Begin
|
||||||
|
DecimalSeparator:=',';
|
||||||
|
Testit;
|
||||||
|
DecimalSeparator:='.';
|
||||||
|
Testit;
|
||||||
|
End.
|
37
docs/sysutex/ex91.pp
Normal file
37
docs/sysutex/ex91.pp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
Program Example91;
|
||||||
|
|
||||||
|
{ This program demonstrates the TextToFloat function }
|
||||||
|
{$mode objfpc}
|
||||||
|
{$h+ }
|
||||||
|
|
||||||
|
Uses SysUtils;
|
||||||
|
|
||||||
|
Const
|
||||||
|
NrValues = 5;
|
||||||
|
TestStr : Array[1..NrValues] of pchar =
|
||||||
|
('1,1','-0,2','1,2E-4','0','1E4');
|
||||||
|
|
||||||
|
Procedure Testit;
|
||||||
|
|
||||||
|
Var
|
||||||
|
I : Integer;
|
||||||
|
E : Extended;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Writeln('Using DecimalSeparator : ',DecimalSeparator);
|
||||||
|
For I:=1 to NrValues do
|
||||||
|
begin
|
||||||
|
Writeln('Converting : ',TestStr[i]);
|
||||||
|
If TextToFloat(TestStr[i],E) then
|
||||||
|
Writeln('Converted value : ',E)
|
||||||
|
else
|
||||||
|
Writeln('Unable to convert value.');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
Begin
|
||||||
|
DecimalSeparator:=',';
|
||||||
|
Testit;
|
||||||
|
DecimalSeparator:='.';
|
||||||
|
Testit;
|
||||||
|
End.
|
Loading…
Reference in New Issue
Block a user