Page 1 of 1

### Fornatting Decimal in Pascal

Posted: Tue Nov 16, 2004 11:21 pm
Are there any effective ways of formatting decimal numbers (double) in pascal?
Eg: 3.1245114411 -> 3.12 or 3.125, etc....

Posted: Tue Jan 11, 2005 9:36 pm
at first sorry for my english 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.

Posted: Tue Jan 11, 2005 9:52 pm
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

### Some code.

Posted: Wed Jan 12, 2005 5:45 am
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!