Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 answers

573 users

How to format the output of a double in Pascal

1 Answer

0 votes
program FormatDouble;

var
  myDouble: Double;
begin
  myDouble := 25.0; // This is internally 2.5E+01
  Writeln('Default scientific notation: ', myDouble);

  // To show as 25.0 (one decimal place, automatic width)
  Writeln('Formatted to 1 decimal place: ', myDouble:0:1);

  // To show as 25.00 (two decimal places, automatic width)
  Writeln('Formatted to 2 decimal places: ', myDouble:0:2);

  // To show as 25 (zero decimal places, truncates fraction)
  Writeln('Formatted to 0 decimal places: ', myDouble:0:0);

  // Example with a number that isn't an exact integer
  myDouble := 123.45678;
  Writeln('Default scientific notation: ', myDouble);
  Writeln('Formatted to 2 decimal places: ', myDouble:0:2); // Output: 123.46 (rounds)
  Writeln('Formatted to 4 decimal places: ', myDouble:0:4); // Output: 123.4568

  // Example pads with spaces if needed
  myDouble := 9.5;
  Writeln('Padded with total width 10, 2 dec: ', myDouble:10:2); // Output:       9.50
end.



(*
run:

Default scientific notation:  2.5000000000000000E+001
Formatted to 1 decimal place: 25.0
Formatted to 2 decimal places: 25.00
Formatted to 0 decimal places: 25
Default scientific notation:  1.2345677999999999E+002
Formatted to 2 decimal places: 123.46
Formatted to 4 decimal places: 123.4568
Padded with total width 10, 2 dec:       9.50

*)

 



answered Jun 11, 2025 by avibootz
...