[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to truncate float number after dot?

Marcin Tyman

7/26/2007 9:06:00 AM

Hi,

How to truncate float number after dot? I suppose it would be easy.

Thank in advance
MT
--
Posted via http://www.ruby-....

7 Answers

hemant

7/26/2007 9:13:00 AM

0

On 7/26/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> Hi,
>
> How to truncate float number after dot? I suppose it would be easy.
>
> Thank in advance

19.84.round #=> big brother is watching you

Pat Maddox

7/26/2007 9:14:00 AM

0

On 7/26/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> Hi,
>
> How to truncate float number after dot? I suppose it would be easy.
>
> Thank in advance
> MT
> --
> Posted via http://www.ruby-....
>
>


irb(main):001:0> 12.68.floor
=> 12
irb(main):002:0> 12.68.ceil
=> 13

Pat

Pat Maddox

7/26/2007 9:15:00 AM

0

On 7/26/07, Pat Maddox <pergesu@gmail.com> wrote:
> On 7/26/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> > Hi,
> >
> > How to truncate float number after dot? I suppose it would be easy.
> >
> > Thank in advance
> > MT
> > --
> > Posted via http://www.ruby-....
> >
> >
>
>
> irb(main):001:0> 12.68.floor
> => 12
> irb(main):002:0> 12.68.ceil
> => 13
>
> Pat
>

Gah, I forgot the all important round!

irb(main):003:0> 12.68.round
=> 13
irb(main):004:0> 12.38.round
=> 12

Pat

Robert Klemme

7/26/2007 9:28:00 AM

0

2007/7/26, Pat Maddox <pergesu@gmail.com>:
> On 7/26/07, Pat Maddox <pergesu@gmail.com> wrote:
> > On 7/26/07, Marcin Tyman <m.tyman@interia.pl> wrote:
> > > Hi,
> > >
> > > How to truncate float number after dot? I suppose it would be easy.
> > >
> > > Thank in advance
> > > MT
> > > --
> >
> > irb(main):001:0> 12.68.floor
> > => 12
> > irb(main):002:0> 12.68.ceil
> > => 13
> >
> > Pat
> >
>
> Gah, I forgot the all important round!
>
> irb(main):003:0> 12.68.round
> => 13
> irb(main):004:0> 12.38.round
> => 12

12.38.to_i

robert

Marcin Tyman

7/26/2007 10:53:00 AM

0

I didn't mean to truncate all. But i.e. to round to several places after
dot.
--
Posted via http://www.ruby-....

Robert Klemme

7/26/2007 11:04:00 AM

0

2007/7/26, Marcin Tyman <m.tyman@interia.pl>:
> I didn't mean to truncate all. But i.e. to round to several places after
> dot.

Usually it's better to calculate with full precision and round when
printing, e.g. using printf of

irb(main):022:0> "%4.1f" % 1.0
=> " 1.0"
irb(main):023:0> "%4.1f" % 1.04
=> " 1.0"
irb(main):024:0> "%4.1f" % 1.05
=> " 1.1"

Kind regards

robert

Josef 'Jupp' Schugt

7/26/2007 2:20:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Marcin Tyman wrote:
> How to truncate float number after dot? I suppose it would be easy.

Rounding to a certain number of digits works as follows:

Let n be the number of digits following the decimal point you want to
obtain. Then

1. Multiply the number by 10 to the power n (i.e. by 10.0 ** n)
2. Round the number (i.e. use Float#round)
3. Divide the result by 10 to the power n

The latter can either be done by division by 10.0 ** n or by
multiplication by 0.1 ** n.

But as Robert Klemme already noted: It rarely makes sense to actually
round numbers. It usually makes more sense to compute with the available
precision and limit the output to the wanted number of digits.

Anyway, sometimes it makes sense. For example if you are required by law
to round intermediate calculation results to a certain amount of digits.
But in such cases one should not at all use floating point numbers for
the computations. One would rather use integers, fixed-point arithmetics
(as e.g. supported by COBOL) or the Decimal data type from Microsoft's
Common Type System (e.g. supported by C#).

Josef 'Jupp' Schugt
- --
Blog available at http://www.mynetcologne.de/~nc-schu...
PGP key with id 6CC6574F available at http://wwwkeys.d...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail....

iD8DBQFGqK1/rhv7B2zGV08RArEPAJ0Z6ihtrVLvBn0ODryW8Ptu3uqplACfTuT1
muYIWqBXnWKO4ZwEKYMiVik=
=5H5E
-----END PGP SIGNATURE-----