comp.lang.ada
 help / color / mirror / Atom feed
* Conversions
@ 2000-03-16  0:00 Karlene Johnson
  2000-03-16  0:00 ` Conversions Marin D. Condic
  0 siblings, 1 reply; 5+ messages in thread
From: Karlene Johnson @ 2000-03-16  0:00 UTC (permalink / raw)


I am trying to convert an Float number to an Integer number, after a
calculation with 2 Float numbers.  Any suggesions on how to convert??




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Conversions
  2000-03-16  0:00 Conversions Karlene Johnson
@ 2000-03-16  0:00 ` Marin D. Condic
  0 siblings, 0 replies; 5+ messages in thread
From: Marin D. Condic @ 2000-03-16  0:00 UTC (permalink / raw)


Karlene Johnson wrote:
> 
> I am trying to convert an Float number to an Integer number, after a
> calculation with 2 Float numbers.  Any suggesions on how to convert??

X  : Float := 1.234 ;
Y  : Integer := 0 ;
....

Y := Integer (X) ; -- Y should now contain the value "1"

There are a number of ways of handling truncation & rounding, but this
is the plain vanilla way to do it. The type name acts as a function to
perform the conversion. However, it only works with "compatible" types.
(You can't for example, convert a String to a Float this way.)

MDC
-- 
=============================================================
Marin David Condic   - Quadrus Corporation -   1.800.555.3393
1015-116 Atlantic Boulevard, Atlantic Beach, FL 32233
http://www.quadruscorp.com/
m c o n d i c @ q u a d r u s c o r p . c o m

***PLEASE REMOVE THE "-NOSPAM" PART OF MY RETURN ADDRESS***

Visit my web site at:  http://www.mcondic.com/

"Because that's where they keep the money."
    --  Willie Sutton when asked why he robbed banks. 
=============================================================




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Conversions
  2009-10-08  9:58   ` 
@ 2009-10-26 19:27     ` Bruno
  2009-10-26 20:09       ` Conversions Jeffrey R. Carter
  2009-10-26 20:19       ` Conversions Adam Beneschan
  0 siblings, 2 replies; 5+ messages in thread
From: Bruno @ 2009-10-26 19:27 UTC (permalink / raw)


Hi all,

I have a little problem with a function that "convert" a date, that is a
record type, into a string. To do this I'm using "image" attribute how
can you see in the following code

http://paste.ideaslabs.com/show/5KEJUGcDF

but when run the program and the function is executed a get an
"constraint error". The compiler show me a message that say that the
size is too long but I don't know what to do.

How can I repair this error?

Thanks,




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Conversions
  2009-10-26 19:27     ` Conversions Bruno
@ 2009-10-26 20:09       ` Jeffrey R. Carter
  2009-10-26 20:19       ` Conversions Adam Beneschan
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2009-10-26 20:09 UTC (permalink / raw)


Bruno wrote:
> 
> I have a little problem with a function that "convert" a date, that is a
> record type, into a string. To do this I'm using "image" attribute how
> can you see in the following code
> 
> http://paste.ideaslabs.com/show/5KEJUGcDF
> 
> but when run the program and the function is executed a get an
> "constraint error". The compiler show me a message that say that the
> size is too long but I don't know what to do.

'Image for an integer type returns the shortest string necessary to represent 
the value, with ' ' added to the front for a positive value and '-' for a 
negative value.

Thus we can get

6   => " 6"
-6  => "-6"
10  => " 10"
-10 => "-10"

For your application (date image), 2-digit day and month numbers are likely; 
their 'Image will not fit in 2 characters.

You can save the 'Image and decide what to do with it:

declare
    Day_Image : constant String := Integer'Image (F.Dia);
begin
    if F.Dia < 10 then
       S (1) := '0';
       S (2) := Day_Image (Day_Image'Last);
    else
       S (1 .. 2) := Day_Image (Day_Image'Last - 1 .. Day_Image'Last);
    end if;
end;

You can also look at the operations in Ada.Text_IO.Integer_IO that write to a 
String parameter.

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail
17



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Conversions
  2009-10-26 19:27     ` Conversions Bruno
  2009-10-26 20:09       ` Conversions Jeffrey R. Carter
@ 2009-10-26 20:19       ` Adam Beneschan
  1 sibling, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2009-10-26 20:19 UTC (permalink / raw)


On Oct 26, 12:27 pm, Bruno <brunomend...@gmail.com> wrote:
> Hi all,
>
> I have a little problem with a function that "convert" a date, that is a
> record type, into a string. To do this I'm using "image" attribute how
> can you see in the following code
>
> http://paste.ideaslabs.com/show/5KEJUGcDF
>
> but when run the program and the function is executed a get an
> "constraint error". The compiler show me a message that say that the
> size is too long but I don't know what to do.

'Image returns a string whose first character is a space if the
argument is not negative.  So if N = 12, then Integer'Image(N) = "
12"---i.e. 3 characters.  That won't fit in the two-character slice
you're trying to put it in, so you get a Constraint_Error.

But just getting rid of the space doesn't work either.  If N = 1, then
Integer'Image(N) = " 1", and if you were able to trim off the space,
the result would be "1", and you'd still get a Constraint_Error.  When
you assign a slice like this, the left and right sides must be the
same length.

There are a number of ways to do what you want, but I'd suggest
looking at the Ada.Strings.Fixed package, especially the Move, Trim,
and Tail routines.

Hope this helps,

                                    -- Adam




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-10-26 20:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-16  0:00 Conversions Karlene Johnson
2000-03-16  0:00 ` Conversions Marin D. Condic
  -- strict thread matches above, loose matches on Subject: below --
2009-10-07 12:40 Ada.Directories.Base_Name and dot files 
2009-10-08  9:39 ` Stephen Leake
2009-10-08  9:58   ` 
2009-10-26 19:27     ` Conversions Bruno
2009-10-26 20:09       ` Conversions Jeffrey R. Carter
2009-10-26 20:19       ` Conversions Adam Beneschan

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