comp.lang.ada
 help / color / mirror / Atom feed
* rounding floating point numbers
@ 2002-02-20 22:17 Fraz
  2002-02-20 22:53 ` Marin David Condic
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Fraz @ 2002-02-20 22:17 UTC (permalink / raw)


Hi

How do i round a floating number to 2 decimal places?  I know u can use
Fore, Aft, Exp in the PUT command, but i want to pass the number as
a paramater to something else, but i only want it to 2 decimal places? is
there a way to store the number like this in a variable?

Fraz





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

* Re: rounding floating point numbers
  2002-02-20 22:17 rounding floating point numbers Fraz
@ 2002-02-20 22:53 ` Marin David Condic
  2002-02-20 23:24 ` David C. Hoos
  2002-02-21  2:29 ` Steve Doiel
  2 siblings, 0 replies; 4+ messages in thread
From: Marin David Condic @ 2002-02-20 22:53 UTC (permalink / raw)


Multiply by one hundred & convert to integer. Convert the integer back to
float and divide by one hundred.

Or you can invent a fixed point type with appropriate precision and do some
type conversions. Depends a lot on what sort of features you need the
numbers to have....

You can also find lots of help doing things like rounding, truncation,
floor, ceiling, etc., by looking in the "Annex K: Language-Defined
Attributes" in the ARM.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Fraz" <fraz@totalise.co.uk> wrote in message
news:yfVc8.96077$as2.15328940@news6-win.server.ntlworld.com...
> Hi
>
> How do i round a floating number to 2 decimal places?  I know u can use
> Fore, Aft, Exp in the PUT command, but i want to pass the number as
> a paramater to something else, but i only want it to 2 decimal places? is
> there a way to store the number like this in a variable?
>
> Fraz
>
>





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

* Re: rounding floating point numbers
  2002-02-20 22:17 rounding floating point numbers Fraz
  2002-02-20 22:53 ` Marin David Condic
@ 2002-02-20 23:24 ` David C. Hoos
  2002-02-21  2:29 ` Steve Doiel
  2 siblings, 0 replies; 4+ messages in thread
From: David C. Hoos @ 2002-02-20 23:24 UTC (permalink / raw)


Bear in mind that floating point numbers cannot exactly represent
fractional decimal values, unless the fractional part happens to be
an integer multiple of a negative integer power of 2 -- e.g. 0.5,
0.25, 0.75, etc.

Further, the maximum number of digits approximately expressible
in floating point representation is constrained by the declaration
of the floating point type -- e.g., type My_Float is digits 6;

Are you sure you want to use floating point?  Have you considered
decimal Fixed_Point types?
  
----- Original Message ----- 
From: "Fraz" <fraz@totalise.co.uk>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Wednesday, February 20, 2002 4:17 PM
Subject: rounding floating point numbers


> Hi
> 
> How do i round a floating number to 2 decimal places?  I know u can use
> Fore, Aft, Exp in the PUT command, but i want to pass the number as
> a paramater to something else, but i only want it to 2 decimal places? is
> there a way to store the number like this in a variable?
> 
> Fraz
> 
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 




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

* Re: rounding floating point numbers
  2002-02-20 22:17 rounding floating point numbers Fraz
  2002-02-20 22:53 ` Marin David Condic
  2002-02-20 23:24 ` David C. Hoos
@ 2002-02-21  2:29 ` Steve Doiel
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Doiel @ 2002-02-21  2:29 UTC (permalink / raw)


"Fraz" <fraz@totalise.co.uk> wrote in message
news:yfVc8.96077$as2.15328940@news6-win.server.ntlworld.com...
> Hi
>
> How do i round a floating number to 2 decimal places?  I know u can use
> Fore, Aft, Exp in the PUT command, but i want to pass the number as
> a paramater to something else, but i only want it to 2 decimal places? is
> there a way to store the number like this in a variable?
>
> Fraz
>

The sample below may approximate what you're looking for:

=============== Start of Sample =========================

with Ada.Float_Text_Io;
with Ada.Text_Io;
procedure roundto2 is

  a : float;
begin
  --------------------------------------------------------------------------
--
  -- Give "a" a value that has more than 2 places past the decimal
  --------------------------------------------------------------------------
--
  a := 3.1415926;
  --------------------------------------------------------------------------
--
  -- Display the value showing 4 places past the decimal
  --------------------------------------------------------------------------
--
  Ada.Text_Io.Put( "Before Rounding: " );
  Ada.Float_Text_Io.Put( a, 2, 4, 0 );
  Ada.Text_Io.New_Line;
  --------------------------------------------------------------------------
--
  -- Round to two places past the decimal (for practical purposes)
  --------------------------------------------------------------------------
--
  a := float'rounding( a * 100.0 ) * 0.01;
  --------------------------------------------------------------------------
--
  -- Display the value again showing 4 places past the decimal
  --------------------------------------------------------------------------
--
  Ada.Text_Io.Put( "After Rounding: " );
  Ada.Float_Text_Io.Put( a, 2, 4, 0 );
  Ada.Text_Io.New_Line;
end roundto2;
================= End of sample ========================

I hope this helps,
SteveD






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

end of thread, other threads:[~2002-02-21  2:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-20 22:17 rounding floating point numbers Fraz
2002-02-20 22:53 ` Marin David Condic
2002-02-20 23:24 ` David C. Hoos
2002-02-21  2:29 ` Steve Doiel

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