Are there any effective ways of formatting decimal numbers (double) in pascal?
Eg: 3.1245114411 -> 3.12 or 3.125, etc....
Fornatting Decimal in Pascal
Moderator: Board moderators
-
- New poster
- Posts: 32
- Joined: Fri Oct 01, 2004 10:53 pm
Some code.
Greetings!
Everytime I learn something new, I try to code it in different ways.
Try something like this:
Code:
Output:
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!
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.
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 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!
_.