Round off float value

Hi folks,

I want to round off 179.9485 to 179.95
round,floor,ceil gives me full integer.
can anyone suggest me?

Thanks in Adavance
Saravanan.K

Hi,

you can do like this:

number = 179.9485
printf(’%.2f’, number)

this one prints out 179.95

Olavi Ivask

Saravanan K. wrote:

Olavi Ivask

Thanks for your instant reply.

here i want to store that value not to print.

floated = (number * 100).round.to_f / 100

Olavi

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

olavi ivask wrote:

Saravanan K. wrote:

Olavi Ivask

Thanks for your instant reply.

here i want to store that value not to print.

floated = (number * 100).round.to_f / 100

Olavi

Thanks Olavi its working fine.

Olavi

How about *100 then round then divide by 100? KISS!

On Dec 19, 7:54 am, Saravanan K. [email protected] wrote:

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.

On 12/19/07, Dejan D. [email protected] wrote:

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs