Member Avatar for gexecuter

So i am trying making a simple calculator in pascal but i am having trouble adding fractions. Like for example i am trying to add 1.5 and 1 so the result should be 2.5 but the output i get is something like 2.5000000e+00 something like that.

How can i change the code so that it outputs a 2.5?

program fractionsum;

uses crt;

 var

 a,b:real;

 c:real;

 begin
 clrscr;

 a:=1.5;
 b:=1;
 c:=a+b;



 writeln (c);

 readkey;
 end.

Recommended Answers

All 4 Replies

You'll have to try something like writeln(c:5:2), to output c with 2 digits of decimal places.

Member Avatar for gexecuter

You'll have to try something like writeln(c:5:2), to output c with 2 digits of decimal places.

That worked, but if i put 1.5 and 1.5 i get 3.00

Is there a way to only display the 3 in that case?

Typically I use FormatFloat. Are you displaying to a set maximum number of digits?

Say you're displaying a maximum of 5 digits, try:

Writeln(FormatFloat('#.###', num));

One of the nice things with FormatFloat is that with the various options for you float format, you can add options into your application (like a ',' as a 1000s seperator, or forcing you display to show a number of 0s after the decimal, etc).

Member Avatar for gexecuter

Typically I use FormatFloat. Are you displaying to a set maximum number of digits?

Say you're displaying a maximum of 5 digits, try:

Writeln(FormatFloat('#.###', num));

One of the nice things with FormatFloat is that with the various options for you float format, you can add options into your application (like a ',' as a 1000s seperator, or forcing you display to show a number of 0s after the decimal, etc).

I am using free pascal and formatfloat gives me an error when compiling, weird.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.