Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   Calculating error (http://www.programmingforums.org/showthread.php?t=11294)

irish Sep 11th, 2006 9:12 AM

Calculating error
 
I've been learning pascal recently and i cant seem to do simple calculations correctly.

Example:

:

program        calculate;

var
        cost:        real;
        people:        integer;

begin
        Writeln('How many people?');
        Readln(people):
        Writeln('How much per person?');
        Readln(cost);
        Writeln('Total cost: ', people*cost);
        Readln;
end.


Returns a value of 1.800000000Ex01

Any ideas?

Thanks in advance.

Eoin Sep 11th, 2006 9:45 AM

Depending on the input of course there is probably nothing wrong with that output, which has a value of 18 I believe.

I guess where you're having difficulty is in the representation of reals when printed, in this case in exponential or scientific format. Unfortunatly I don't know pascal to suggest a way of changing the output but I'm sure someone here will :) .

irish Sep 19th, 2006 5:14 AM

Yea found the problem.

Needed to show how many digits:decimal places i wanted to display:

:

Writeln('Total cost: ', people*cost:6:2);

Thanks anyway Eoin :)


All times are GMT -5. The time now is 1:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC