From: Lucky Nl on 26 May 2010 08:41 Hi , I want to round the my floating point result into nearest halfpoint how to do it. Example: 1)My floating point result is 3.3 i want it become 3.5 2) My floating point result is 3.6 i want it become 4 How can we do it -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 26 May 2010 09:09 2010/5/26 Lucky Nl <lakshmi27.u(a)gmail.com>: > I want to round the my floating point result into nearest halfpoint > .how to do it. > Example: > 1)My floating point result is 3.3 > i want it become 3.5 > 2) My floating point result is 3.6 > i want it become 4 If you are rounding then 3.6 should yield 3.5 because (3.6 - 3.5) << (4 - 3.6). Otherwise you want the ceiling function. > How can we do it irb(main):012:0> [3.3, 3.6].map {|x| (x * 2).round / 2.0} => [3.5, 3.5] irb(main):014:0> [3.3, 3.6].map {|x| (x * 2).ceil / 2.0} => [3.5, 4.0] Btw, I would rather not round float values and use them as they are. If at all I would usually only round them for output. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Benoit Daloze on 26 May 2010 09:13 On 26 May 2010 15:09, Robert Klemme <shortcutter(a)googlemail.com> wrote: > > robert > Arf, you answered before I did finish my answer. .. was looking the exact names for rounding in BigDecimal.mode .. Regards, B.D.
|
Pages: 1 Prev: How to call method in the another file Next: [ANN] Money 3.0.1 |