comp.lang.ada
 help / color / mirror / Atom feed
From: gautier_niouzes@hotmail.com
Subject: Re: Noob question..  Autopromotion?
Date: Tue, 6 May 2014 06:31:13 -0700 (PDT)
Date: 2014-05-06T06:31:13-07:00	[thread overview]
Message-ID: <ade7fae8-8b72-4530-aece-644425c0dfae@googlegroups.com> (raw)
In-Reply-To: <9ac10bab-2ade-4ac8-a99b-336595e38f67@googlegroups.com>

Hello,
As Adam said, a figure read as a literal in the Ada source code and a figure read from a string from an user are different topics. Typically figures with or without .0 are valid (to GNAT and OA run-time libraries at least).

   with ada.text_io;
   procedure value is
   begin
     ada.text_io.put_line(float'image(float'Value("1234.0")));
     ada.text_io.put_line(float'image(float'Value("1234")));
   end;

Now, you may have user inputs with thousands separators and/or percents, for instance - especially if the input is actually a slight modification of an output). So you will certainly need a custom solution anyway.
Here is a function dealing with both (filtering thousands separators, and doing *0.01 with percent signs):

  function Value(s: String) return Real is
    t: constant String:= Trim(s, Both);
    ft: String(t'Range);
    j: Integer;
  begin
    if t'Length > 0 and then t(t'Last) = '%' then
      return Value(t(t'First .. t'Last-1)) * 0.01;
    elsif Index(t, (1 => thousands_separator)) = 0 then
      return Real'Value(t);
    else -- filter all thousands separators...
      j:= ft'First - 1;
      for i in t'Range loop
        if t(i) /= thousands_separator then
          j:= j + 1;
          ft(j):= t(i);
        end if;
      end loop;
      return Value(ft(ft'First .. j));
    end if;
  end Value;
_________________________
Gautier's Ada programming
http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address 


  parent reply	other threads:[~2014-05-06 13:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-05 14:22 Noob question.. Autopromotion? sdalemorrey
2014-05-05 14:54 ` Adam Beneschan
2014-05-05 15:32 ` Nasser M. Abbasi
2014-05-05 19:30   ` Simon Wright
2014-05-06 13:31 ` gautier_niouzes [this message]
2014-05-07 16:13   ` jpwoodruff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox