Fornatting Decimal in Pascal

Write here if you have problems with your Pascal source code

Moderator: Board moderators

Post Reply
randomtaiwanese
New poster
Posts: 32
Joined: Fri Oct 01, 2004 10:53 pm

Fornatting Decimal in Pascal

Post by randomtaiwanese »

Are there any effective ways of formatting decimal numbers (double) in pascal?
Eg: 3.1245114411 -> 3.12 or 3.125, etc....
*luna*
New poster
Posts: 6
Joined: Tue Jan 11, 2005 9:12 pm
Location: Slovenia
Contact:

Post by *luna* »

at first sorry for my english :wink:

i don't know if i understood you but if you wanna 3.1245114411 write like 3.12 or 3.125, you can use this.

x:=3.1245114411;
write(x:1:2); for 3.12
and
write(x:1:3); for 3.125

i hope i helped you.
Cahoun
New poster
Posts: 13
Joined: Mon Jan 03, 2005 2:34 pm
Location: Czech Republic
Contact:

Post by Cahoun »

Or you can try as *luna* wrote :
I changed little bit.
x:=3.1245114411;
write(x:0:2); for 3.12
and
write(x:0:3); for 3.125
Learn, learn, learn.
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Some code.

Post by _.B._ »

Greetings!
Everytime I learn something new, I try to code it in different ways.
Try something like this:

Code:

Code: Select all

program Formato;

begin
  writeLn(pi:0:0);
  writeLn(pi:0:1);
  writeLn(pi:0:2);
  writeLn(pi:0:3);
  writeLn(pi:0:4);
  writeLn(pi:0:5);
  writeLn(pi:0:6);
  writeLn(pi:0:7);
  writeLn;
  writeLn(pi:0:2);
  writeLn(pi:1:2);
  writeLn(pi:2:2);
  writeLn(pi:3:2);
  writeLn(pi:4:2);
  writeLn(pi:5:2);
  writeLn(pi:6:2);
  writeLn(pi:7:2)
end.
Output:

Code: Select all

3
3.1
3.14
3.142
3.1416
3.14159
3.141593
3.1415927

3.14
3.14
3.14
3.14
3.14
 3.14
  3.14
   3.14
For integer, char and string types, you can use one number for the output format (variable:number, value:number, 'a':8, 54:4), that will "save" that number of spaces for the value you want to print. If the length is larger than the number you chose, it will ignore it (just like if you do a value:0).
For real type, you can use 2 numbers. The first one for the same porpouse, to save the space for it (including point and decimals), and a second number for the number of decimals you want to print after the point (variable:space:decimals, value:space:decimals).

Keep posting!
_.
Post Reply

Return to “Pascal”