Member Avatar for Member #169075

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.

Dani AI

Generated

— the simplest idea is to control how the number is converted to text instead of relying on the compiler's default formatting. 's suggestion to force a fixed number of decimals will work, and was right that the Format/FormatFloat family gives more control — in Free Pascal those routines live in the SysUtils unit, so you must add SysUtils to your uses clause or you'll get the compile error you saw.

A convenient pattern that handles both cases (show decimals when needed, drop them when the result is an integer) is: format the value with a fixed number of decimal places and then trim trailing zeros and a trailing decimal point. The helper below shows that approach; include SysUtils in uses so FloatToStrF is available.

uses crt, SysUtils;

function TrimFloat(Value: Extended; MaxDec: Integer): string;
var
  s: string;
begin
  s := FloatToStrF(Value, ffFixed, 18, MaxDec);   // fixed format with MaxDec digits
  while (Length(s) > 0) and (s[Length(s)] = '0') do
    Delete(s, Length(s), 1);
  if (Length(s) > 0) and (s[Length(s)] = '.') then
    Delete(s, Length(s), 1);
  Result := s;
end;

Notes: choose MaxDec large enough for the precision you need (10 is usually safe) and be aware of floating-point rounding — values like 3.0000000000000004 may not trim perfectly unless you round first or pick an appropriate MaxDec. If you always want a fixed number of decimals (for example always show two places), use the formatting functions directly without trimming.

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 Member #169075

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 Member #169075

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.