How to transform integer to float and vice-versa i

2019-08-01 06:15发布

问题:

Is there some kind of implemented function that would allow to transform an integer to float and vice versa?

I managed to write a short function that transforms an integer to float:

function Transform(First: Integer) return Float is
    A: Integer := First;
    B: Float := 0.0;
begin
    For_Loop:
    for I in Integer range 1 .. A loop
        B := B + 1.0;
    end loop For_Loop;
    return B;
end Transform;

But I don't know how to go from Float to Integer.

回答1:

Ada can do explicit type conversions

with Ada.Text_IO; use Ada.Text_IO;
procedure Convert is
  A: Integer:= 4;
  B: Float;
  C: Float := 6.8;
  D: Integer;
begin
  B := Float(A);
  Put_Line(Float'Image(B));

  D:= Integer(C);
  Put_Line(Integer'Image(D));
end Convert;


标签: ada