Float to String:字符串长度问题

Float to String: Problem with string length

我想将浮点值转换为字符串并创建了以下简短示例:

with Ada.Text_IO;

procedure Example is
   A : constant Float := -1.234;
   B : constant Float := 123_456.789;
   C : constant Float := 987.654_321;

   package Float_IO is new Ada.Text_IO.Float_IO (Num => Float);

   String_Float_A : String := "     ";
   String_Float_B : String := "          ";
   String_Float_C : String := "       ";
begin
   Ada.Text_IO.Put_Line (Float'Image (A));
   Ada.Text_IO.Put_Line (Float'Image (B));
   Ada.Text_IO.Put_Line (Float'Image (C));

   Float_IO.Put
     (To   => String_Float_A,
      Item => A,
      Aft  => 2,
      Exp  => 0);

   Float_IO.Put
     (To   => String_Float_B,
      Item => B,
      Aft  => 2,
      Exp  => 0);

   Float_IO.Put
     (To   => String_Float_C,
      Item => C,
      Aft  => 2,
      Exp  => 0);

   Ada.Text_IO.Put_Line (String_Float_A);
   Ada.Text_IO.Put_Line (String_Float_B);
   Ada.Text_IO.Put_Line (String_Float_C);
end Example;

我的问题:我需要在调用过程 Put 之前创建长度足够的字符串变量。这如何在运行时动态完成?基本上我需要弄清楚点之前的位数。那么足够的字符串长度将是:1 (sign) + Number_Of_Dots + 1 (decimal separator) + Aft.

你可以创建一个函数来完成这项工作,像这样:

with Ada.Text_IO;
with Ada.Strings.Fixed;
procedure Marcello_Float is

   function Image (Item : Float;
                   Aft : Ada.Text_IO.Field := Float'Digits - 1;
                   Exp : Ada.Text_IO.Field := 3) return String
   is
      package Float_IO is new Ada.Text_IO.Float_IO (Float);
      Result : String (1 .. 128);
   begin
      Float_IO.Put (To   => Result,
                    Item => Item,
                    Aft  => Aft,
                    Exp  => Exp);
      return Ada.Strings.Fixed.Trim (Result,
                                     Side => Ada.Strings.Both);
   end Image;

   A : constant Float := -1.234;
   B : constant Float := 123_456.789;
   C : constant Float := 987.654_321;

   A_Image : constant String := Image (A, Aft => 2, Exp => 0);
   B_Image : constant String := Image (B, Aft => 2, Exp => 0);
   C_Image : constant String := Image (C, Aft => 2, Exp => 0);

   use Ada.Text_IO;
begin
   Put_Line (A'Image & " => " & A_Image);
   Put_Line (B'Image & " => " & B_Image);
   Put_Line (C'Image & " => " & C_Image);
end Marcello_Float;

我把 Result 写得长得离谱。我认识到计算一个确切的尺寸实际上可以回答你原来的问题;只是懒惰。

小数点前的位数可以计算为1加上常用对数(以10为底)的整数部分 数字的整数部分。

在艾达中:

    N := Integer (Float'Floor (Log (Float'Floor (abs X), 10.0))) + 1;

您的程序必须带有 Ada.Numerics.Elementary_Functions。 如果数字为负数,则必须为减号添加 space。

如果整数部分为0,则此公式不成立,但N显然为1

您的示例使用 Float,可能作为一些更具体的 real type. If your actual data is better modeled as a decimal fixed point type, discussed here, don't overlook the convenience of Edited Output for Decimal Types, discussed here. The example below uses the string "+Z_ZZZ_ZZ9.99" to construct a picture of the desired output Image.

的代理

控制台:

-        1.23
+  123,456.79
+      987.65
+1,000,000.00

代码:

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Text_IO.Editing; use Ada.Text_IO.Editing;

procedure Editing is

   type Number is delta 0.000_001 digits 12;
   type Numbers is array (Positive range <>) of Number;
   package Number_Output is new Decimal_Output (Number);
   Format_String : constant String  := "+Z_ZZZ_ZZ9.99";
   Format        : constant Picture := To_Picture (Format_String);
   Values        : constant Numbers :=
     (-1.234, 123_456.789, 987.654_321, Number'Last);

begin
   for Value of Values loop
      Put_Line (Number_Output.Image (Value, Format));
   end loop;

编辑结束;