If you want to have exact decimals in Pascal, you have to use the currency type.

program Zadanie2;
var r : currency;
begin
    r:=2.1;
    while r <> 4.3 do
    begin
        r:= r + 0.1;
        writeln(r:0:2);
    end;
end.

Now the output ends at 4.30.

As you can notice, I also changed the format so you don't get the scientific notation.

Code and results: http://ideone.com/Hf65v2

Answer from Andrew on Stack Overflow
🌐
Smart Pascal
smartpascal.github.io › help › assets › floattostrf.htm
Smart Pascal : FloatToStrF command
Object Pascal Reference · A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
🌐
InformIT
informit.com › articles › article.aspx
Integer Types and Floating-Point Types | Basic Pascal Syntax for Kylix Developers | InformIT
Floating-point numbers are sometimes known as decimal numbers, such as 7.0, 3.2, -5.004, and 32,000.0000000034. A whole series of types in Pascal are based on whole numbers. These include the Byte, Integer, Char, and Boolean types.
🌐
Free Pascal
freepascal.org › docs-html › ref › refsu5.html
Real types
Free Pascal uses the math coprocessor (or emulation) for all its floating-point calculations. The Real native type is processor dependent, but it is either a Single or a Double. Only the IEEE floating point types are supported, and these depend on the target processor and emulation options.
🌐
Smart Pascal
smartpascal.github.io › help › assets › formatfloat.htm
Smart Pascal : FormatFloat command
Object Pascal Reference · A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
🌐
Free Pascal
freepascal.org › docs-html › rtl › math › float.html
Float
All calculations are done with the Float type which is the largest float type available for the current CPU. This allows to recompile the unit with a different float type to obtain a desired precision.
🌐
ModdingWiki
moddingwiki.shikadi.net › wiki › Turbo_Pascal_Real
Turbo Pascal Real - ModdingWiki
Real variable types were frequently used in the early versions of Pascal because it did not have native support for 32-bit integers until later versions, so, if you wanted to store a number greater than 32,767 or a number with a decimal value, you had to use a real. 32 and 64-bit floats were introduced in Turbo Pascal version 5.
🌐
Programming Idioms
programming-idioms.org › idiom › 79 › convert-integer-to-floating-point-number › 1027 › pascal
Convert integer to floating point number, in Pascal
Pascal · var y: single; begin y := x; end; Demo · Ada · C · Caml · Clojure · C++ C++ C# D · Dart · Elixir · Fortran · Go · Haskell · JS · Java · Java · Java · Java · Lisp · Lua · Lua · PHP · Perl · Python · Ruby · Rust · Y : constant Float := Float (X); float y = (float)x; let y = float_of_int(x) (def y (float x)) Doc ·
Find elsewhere
🌐
Free Pascal
freepascal.org › docs-html › prog › progsu158.html
Floating point types - Free Pascal
Floating point types have a storage binary format divided into three distinct fields : the mantissa, the exponent and the sign bit which stores the sign of the floating point value.
Top answer
1 of 3
2

The Format() function is normally used in situations like this.

WriteLn(Format('%*.*f', [0, dec, your_real_number]));

*.* is interpreted as total_characters.decimal_digits. Passing zero for the first means that width is adjusted according to how large your real is. The number of decimals can be a variable (dec), which you can adjust to your specification.


Update:

You mention that you want an exact representation of a float with respect to the number of decimals.

As mentioned in my comment, most floating point values does not have a finite number of decimals. And most decimal fractions cannot be represented by a binary type.

There are some libraries that can handle floating point values of arbitrary size. See TBigFloat for example. There is a formatting routine that can strip redundant zeroes from a decimal float.


Still, there is a possibility to remove trailing zeroes by using the general format specifier:

WriteLn(Format('%g',[your_real_number]));

You can specify the width and the number of significant digits as well.

2 of 3
2

For example, if you have input x=1.51 in real variable type, then you write only writeln(x), the output will be 1.5100000000. If you write writeln(x:0:3), the output will be 1.510 (3 digits after ".") ...

var x: real;
Begin
x:=1.51;
writeln(x); //output 1.5100000000
writeln(x:0:4); //output 1.5100 (4 digits after ".")
writeln(x:0:2); //output 1.51 (2 digits after ".")
readln;
End.

From your other example, if your input is 1.512426, with writeln(x:0:5) it will only show 5 digits after "." and the output will be 1.51242

var x: real;
Begin
x:=1.512426;
writeln(x); //output 1.5124260000
writeln(x:0:4); //output 1.5124 (4 digits after ".")
writeln(x:0:2); //output 1.51 (2 digits after ".")
readln;
End. 

So, if you write writeln(x:0:dec) the output will be "dec" digits after "."

Hope this helps, I'm just trying to answer from a different perspective.

Top answer
1 of 4
5

To actually convert:

var
  i: integer;
...
  i := round(floatVar);

To output only the integer part:

writeln(floatVar:9:0);
2 of 4
1

For purpose of showing pretty output on the screen you can use something like this:

Writeln(result:0:2);

Result on screen would be this:

9.00

What this means someone would ask? Well first number 0 means how wide filed is. if you say it's 0 then Pascal writes it at the very left side of screen. If you said writeln(result:5:2) result would be:

   9.00

In other words i would print form the right side and leave 5 chars to do so.

Second number 2, in this example means you want that result printed with 2 decimal places. You can place it only if you want to print on screen value that is real, single, double, extended and so on.You can round to any number of decimals, and if you do writeln(result:0:0) you would get ouput:

9

If you are printing integer and want to have some length of field, lets sat 5 you would do: writeln(int:5). If you added :2 to the end you would get compile time error.

This all also works for something like this: writeln(5/3.5+sqrt(3):0:3),

You should know that this does not round variable itself but just formats output. This is also legal:

program test;
var
    a:real;
    n,m:integer;
begin
    readln(a,m,n);
    writeln(a:m:n);
end.

What i did here is i asked user if on how many decimals and with what length of field he wants to write entered number a. This can be useful so i'm pointing it out. Thank you for reading. I hope i helped

🌐
GitHub
github.com › benibela › bigdecimalmath
GitHub - benibela/bigdecimalmath: Pascal library for arbitrary precision BCD floating point numbers
Pascal library for arbitrary precision BCD floating point numbers - benibela/bigdecimalmath
Starred by 27 users
Forked by 15 users
Languages   Pascal
🌐
Free Pascal
freepascal.org › docs-html › rtl › sysutils › strtofloat.html
StrToFloat
StrToFloat converts the string S to a floating point value. S should contain a valid string representation of a floating point value (either in decimal or scientific notation).
🌐
Smart Pascal
smartpascal.github.io › help › assets › floattostr.htm
Smart Pascal : FloatToStr command
Object Pascal Reference · A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
🌐
Free Pascal
freepascal.org › docs-html › rtl › sysutils › formatfloat.html
FormatFloat
FormatFloat formats the floating-point value given by Value using the format specifications in Format.
🌐
University of Edinburgh
dcs.ed.ac.uk › home › SUNWspro › 3.0 › pascal › lang_ref › ref_data.doc.html
Pascal Language Reference: 2 - Data Types
Pascal represents real, single, shortreal, double, and longreal data types according to the IEEE standard, A Standard for Binary Floating-Point Arithmetic.
🌐
TutorialsPoint
tutorialspoint.com › pascal › pascal_variable_types.htm
Pascal - Variable Types
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in Pascal has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be appl
🌐
TutorialsPoint
tutorialspoint.com › pascal › pascal_data_types.htm
Pascal - Data Types
Following table gives you details about standard integer types with its storage sizes and value ranges used in Object Pascal −
🌐
GitHub
github.com › tebe6502 › 16bit-half-float-in-Pascal
GitHub - tebe6502/16bit-half-float-in-Pascal: 16bit half-float in Pascal
16bit half float in Pascal/Delphi https://galfar.vevb.net/wp/2011/16bit-half-float-in-pascaldelphi/
Author   tebe6502