Dear Sir,

I have got confused to the real / integer. Would you mind helping to express it to me?

I have got the answer of the following question:

Write a menu system for the currency exchange program. My menu could look like this:

Exchange rates
How much money would you like to exchange?

Press 1 for Australian dollars
2 for US dollars
3 for English pounds
4 for Japanese yen
5 for Euros
6 for Indian rupees
7 for PNG kina
8 for SA rand

My answer:

program Conversion_with_menuII;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  aus=0.9024;
  us=0.7697;
  eng=0.3803;
  y=0.9144;
  e=0.5657;
  rup=31.17;
  kina=1.6197;
  rand=5.1805;

var
  nz_dollar: real;
  aus_out: real;
  us_out: real;
  eng_out: real;
  y_out: real;
  e_out: real;
  rup_out: real;
  kina_out: real;
  rand_out: real;
  choice: integer;

begin
  write('How much money would you lie to change? :$');
  readln(nz_dollar);
  writeln('Please select which currency you would like to convert to');
  writeln('Press 1 for Australian dollars');
  writeln('Press 2 for US dollars');
  writeln('Press 3 for English pounds');
  writeln('Press 4 for Japanese yen');
  writeln('Press 5 for Euros');
  writeln('Press 6 for Indian rupees');
  writeln('Press 7 for PNG kina');
  writeln('Press 8 for SA rand');
  readln(choice);
case choice of
  1: begin
  aus_out:=nz_dollar*aus;
  writeln('The amount in Australian Dollars is: $',aus_out:6:2);
  end;
  2: begin
  us_out:=nz_dollar*us;
  writeln('The amount in US dollars is: $',us_out:6:2);
  end;
  3: begin
  eng_out:=nz_dollar*eng;
  writeln('The amount in English pounds is: $',eng_out:6:2);
  end;
  4: begin
  y_out:=nz_dollar*y;
  writeln('The amount in Japanese yen is: $',y_out:6:2);
  end;
  5: begin
  e_out:=nz_dollar*e;
  writeln('The amount in Euros is: $',e_out:6:2);
  end;
  6: begin
  rup_out:=nz_dollar*rup;
  writeln('The amount in Indian rupees is $',rup_out:6:2);
  end;
  7: begin
  kina_out:=nz_dollar*kina;
  writeln('The amount in PNG kina is $',kina_out:6:2);
  end;
  8: begin
  rand_out:=nz_dollar*rand;
  writeln('The amount in SA rand is $',rand_out:6:2);
  end;
  end;
  readln;
end.

I have tried to change the real to integer but it is work. Could you express the reason to me please?

Cheers,

Dani AI

Generated

Short answer: do not change your money variables to Integer — you will lose cents. Keep the amounts as Real (or, in Delphi/FreePascal, use the Currency type) so you can represent fractional units correctly. Real/Double hold fractional values; Integer holds whole numbers only. (freepascal.org)

is right to mention Int, Frac, Trunc and Round, but here is the practical difference in one line: Int returns the integer part as a floating value, Frac returns the fractional remainder, Trunc returns the integer part as an integer, and Round returns the nearest integer (note: Round uses banker's rounding). Use Trunc or Round when you explicitly need an integer result; otherwise keep Real. (oreilly.com)

If you want to store whole cents to avoid floating inaccuracies, convert to an integer number of cents and do integer math, then convert back only for display. Example pattern (safe and simple):

cents := Round(nz_dollar * 100);    // total cents as Integer
writeln('Stored cents = ', cents);  // integer arithmetic from here on

Or prefer Currency (fixed‑point scaled type) when available for money calculations. Currency is a 64‑bit scaled type that minimizes typical FP rounding problems. (freepascal.org)

Formatting output (for example :6:2 or :1:0) only changes how the number is printed; it does not change the stored value or its type. If your program “didn’t work” after changing types, check the compiler messages and watch where values are truncated vs. rounded, and watch locale DecimalSeparator when reading user input. (freepascal.org)

Recommended Answers

All 4 Replies

What do you want convert real to integer?

if only so
writeln('The amount in SA rand is $',rand_out:1:0);
and the result seems like integer number like 12 or 34 without decimal point.....
:D

Int will give you the number before the comma in a real number.

program Convert;

var
r: Real;

begin
r := Int(3.14);
end.

Frac will give you the number after the comma in a real number.

program Convert;

var
r: Real;

begin
r := Frac(3.14);
end.

Round will round off a real number to the nearest integer.

program Convert;

var
i: Integer;

begin
i := Round(3.14);
end.

Trunc will give you the number before the comma of a real number as an integer.

program Convert;

var
i: Integer;

begin
i := Trunc(3.14);
end.

enough? :D

Dear Sir,

Thank you for detailed expression.

Cheers,

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.