Recent

Author Topic: Sending to a printer  (Read 2310 times)

FPnewbie

  • Newbie
  • Posts: 6
Sending to a printer
« on: April 18, 2014, 03:40:13 am »
If I am posting this to the wrong place please advise me of the proper area. I am totally new to Free Pascal and am trying to rewrite an old TurboPascal program to create a Windows app. I am really confused when it comes to sending data to a printer. I am reading a record which consists of real numbers from a disk file and wish to send it to a printer.

The line from TPascal is :

   writeln(lst,'text ',var:field width:decimals); 

My line of text needs to look like:

Mydata = xxxx.xx

where "Mydata = " is a string and "xxxx.xx" is a real number represented to two decimals.

Is there a simple way to do this in FP? I can find examples of sending a text string to a printer, but nothing on sending real numbers on a formatted line with a text string. Maybe my reals need to be converted to strings? If so how do I restrict the number of decimals shown.
Like I said, I am a real beginner in both the Lazarus IDE and Free Pacal.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Sending to a printer
« Reply #1 on: April 18, 2014, 06:07:22 am »
there are multiple ways to convert a number to a string take a look on writestr
Code: [Select]
var
  vTst:string;
begin
  WriteStr(vTst, 10.3456:8:2);
  ShowMessage(vTst);
which should be identical with writeln it just outputs the result to a string variable of course the Format function
Code: [Select]
var
  vTst:string;
begin
  vTst := Format('%.2F',[1234.456789]);
  ShowMessage(vTst);
which returns a string formated the way you need it for multiple data types you can have multiple placeholders in the string with different types each and format will replace them all as needed. Keep in mind that the values order must much the place holder's order in the string.
Last but not least is the FloatToStrF method which works just like the format for a single value eg
Code: [Select]
var
  vTst:string;
begin
  vTst := FloatToStrF(1234.456789,ffFixed,ffixed,9999,2);
  ShowMessage(vTst);
Those are the main functions that you can use to convert a float to a string depending on your needs you use any one you fill most comfortable with, after that sending the string to the printer should be something easily solved. Have you discovered Datasets and lazReport yet? In your case I would use lazReport to design a report the way I want it and a virtual dataset to fill it with my data but that might be a bit over the top for a beginner so I'll just let you work your way to what ever comes natural and wait for your next question on the subject.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Sending to a printer
« Reply #2 on: April 18, 2014, 06:38:08 am »
taazz is right when he says "multiple ways",  but forgot my favorite:
formatfloat('#,##0.00;(#,##0.00);""',numeric_variable)
« Last Edit: April 18, 2014, 06:40:03 am by bylaardt »

 

TinyPortal © 2005-2018