comp.lang.ada
 help / color / mirror / Atom feed
* altering number of significant figures
@ 2004-09-28  5:59 David Peterson
  2004-09-28  6:41 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David Peterson @ 2004-09-28  5:59 UTC (permalink / raw)


Hi, sorry for this stupid question, but i can not seem to find out how to do 
it. Suppose i define:


   myfloat : float := 1234.5678;
   screen_display("value = " & float'image(myfloat));


... where screen_display() prints out the content passed to it, to standard 
output.

However I do not know how to only print only the first 2 (for example) 
decimal places of myfloat.

The above will print "value = 1234.5678". However I want "value = 1234.56".

I know there is a simple solution to this, but i cant find it. Using Barnes 
to learn from.

Any help most welcome :)
Thanks,
David 





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

* Re: altering number of significant figures
  2004-09-28  5:59 altering number of significant figures David Peterson
@ 2004-09-28  6:41 ` tmoran
  2004-09-28  6:51 ` Martin Krischik
  2004-09-28 17:58 ` Jeffrey Carter
  2 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2004-09-28  6:41 UTC (permalink / raw)


>  myfloat : float := 1234.5678;
>  screen_display("value = " & float'image(myfloat));
>The above will print "value = 1234.5678". However I want "value = 1234.56".
  I presume you actually want it rounded, not truncated.
  You can use Ada.Text_IO.Float_IO to make a string, with full control
over total length, digits after the point, and E exponent notation.
  Another option is
   type Two_Aft_Digits is delta 0.01 range ...
   screen_display("value = " & Two_Aft_Digits'image(Two_Aft_Digits(myfloat)));



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

* Re: altering number of significant figures
  2004-09-28  5:59 altering number of significant figures David Peterson
  2004-09-28  6:41 ` tmoran
@ 2004-09-28  6:51 ` Martin Krischik
  2004-09-28 17:58 ` Jeffrey Carter
  2 siblings, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2004-09-28  6:51 UTC (permalink / raw)


David Peterson wrote:

> Hi, sorry for this stupid question, but i can not seem to find out how to
> do it. Suppose i define:
> 
> 
>    myfloat : float := 1234.5678;
>    screen_display("value = " & float'image(myfloat));
> 
> 
> ... where screen_display() prints out the content passed to it, to
> standard output.
> 
> However I do not know how to only print only the first 2 (for example)
> decimal places of myfloat.
> 
> The above will print "value = 1234.5678". However I want "value =
> 1234.56".
> 
> I know there is a simple solution to this, but i cant find it. Using
> Barnes to learn from.

The answer is: Ada.Text_IO.Float_IO

It's quite straight foreward - The only supprise is that Fload_IO is
embedded in Ada.Text_IO and not separate.


With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: altering number of significant figures
@ 2004-09-28  6:52 Christoph Karl Walter Grein
  2004-09-28 10:26 ` David Peterson
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Karl Walter Grein @ 2004-09-28  6:52 UTC (permalink / raw)
  To: comp.lang.ada

>    myfloat : float := 1234.5678;
>    screen_display("value = " & float'image(myfloat));

> However I do not know how to only print only the first 2 (for example) 
> decimal places of myfloat.
> 
> The above will print "value = 1234.5678". However I want "value = 1234.56".

Look at RM A.10.1 (67) procedure Put to a String. There you can choose the format (digits before and after the dot, exponential digits). This rounds (1234.57).

If you really want 1234.56, you have to use the 'Floor attribute (RM K(74)).
________________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193




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

* Re: altering number of significant figures
  2004-09-28  6:52 Christoph Karl Walter Grein
@ 2004-09-28 10:26 ` David Peterson
  0 siblings, 0 replies; 6+ messages in thread
From: David Peterson @ 2004-09-28 10:26 UTC (permalink / raw)


thanks so much everyone for your help!

Regards, David 





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

* Re: altering number of significant figures
  2004-09-28  5:59 altering number of significant figures David Peterson
  2004-09-28  6:41 ` tmoran
  2004-09-28  6:51 ` Martin Krischik
@ 2004-09-28 17:58 ` Jeffrey Carter
  2 siblings, 0 replies; 6+ messages in thread
From: Jeffrey Carter @ 2004-09-28 17:58 UTC (permalink / raw)


David Peterson wrote:

> The above will print "value = 1234.5678". However I want "value =
> 1234.56".

Unless there's a problem with your compiler, it should output

"value =  1.2345678E+03"

with 2 spaces after '='.

-- 
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
07




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

end of thread, other threads:[~2004-09-28 17:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-28  5:59 altering number of significant figures David Peterson
2004-09-28  6:41 ` tmoran
2004-09-28  6:51 ` Martin Krischik
2004-09-28 17:58 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2004-09-28  6:52 Christoph Karl Walter Grein
2004-09-28 10:26 ` David Peterson

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