Re: Round off float value
- From: Rick DeNatale <rick.denatale@xxxxxxxxx>
- Date: Wed, 19 Dec 2007 07:10:16 -0500
On 12/19/07, Dejan Dimic <dejan.dimic@xxxxxxxxx> wrote:
On Dec 19, 7:54 am, Saravanan Krishnan <saravan...@xxxxxxxxx> wrote:
olavi ivask wrote:
Hi,
you can do like this:
number = 179.9485
printf('%.2f', number)
this one prints out 179.95
Olavi Ivask
Thanks for your instant reply.
here i want to store that value not to print.
Regards
saravanan.K
--
Posted viahttp://www.ruby-forum.com/.
This is one of interesting problems that are circulated from time to
time around the developers. Most of the times I find the wrong
approach like want to store double value adjusted to two decimals.
Even it is pleasing to us human to see double values rounded in such a
pretty way associate us on money, it has no influence on haw the same
number is stored in PC memory.
The key question is why you need that kind of rounding?
It is usually the presentation or formatting - you want to have nice
output on the screen or paper.
The second is calculating - you need to perform calculation where
values are adjusted on predefined number of decimals.
The most often is both of previous ones.
Storing is never the problem. You should always store the original.
The original can be transform in any way. Reverse transformation is
not always possible.
It is better to kip fraction (number of significant decimals) as
separate information and perform the rounding transformation when
needed and then look to minimize number of transformations.
In the common case that you want to represent a monetary amount in a
decimal currency, (Euro's and cents, Dollars and cents, etc) one
commonly used approach is to store the value as an integer number of
cents, and only convert on input and output.
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
.
- References:
- Round off float value
- From: Saravanan Krishnan
- Re: Round off float value
- From: olavi ivask
- Re: Round off float value
- From: Saravanan Krishnan
- Re: Round off float value
- From: Dejan Dimic
- Round off float value
- Prev by Date: Re: String#[] behaviour
- Next by Date: Re: Partial Search
- Previous by thread: Re: Round off float value
- Next by thread: Re: Round off float value
- Index(es):
Relevant Pages
|