Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to Round off and Truncate in ruby

Reply
Thread Tools

How to Round off and Truncate in ruby

 
 
Surjit Nameirakpam
Guest
Posts: n/a
 
      11-20-2007
Scenario.

I have an number x=12.45678

1. I want a function to display x as a round of upto 3 decimal place as
12.457
2. Similarly truncate x at 3 decimal place as 12.456

I am using gsl library function and searched for TRUNC and ROUND
function but couldn't find anything concrete
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      11-20-2007
On Nov 20, 1:07 pm, Surjit Nameirakpam <(E-Mail Removed)>
wrote:
> Scenario.
>
> I have an number x=12.45678
>
> 1. I want a function to display x as a round of upto 3 decimal place as
> 12.457
> 2. Similarly truncate x at 3 decimal place as 12.456

irb(main):001:0> x=12.45678
=> 12.45678
irb(main):002:0> s = "%.3f" % x
=> "12.457"
irb(main):003:0> y = (x*1000).round / 1000
=> 12
irb(main):004:0> y = (x*1000).round / 1000.0
=> 12.457
irb(main):007:0> class Numeric
irb(main):008:1> def round_to( decimals=0 )
irb(main):010:2> factor = 10.0**decimals
irb(main):011:2> (self*factor).round / factor
irb(main):012:2> end
irb(main):013:1> end
=> nil
irb(main):014:0> y = x.round_to(3)
=> 12.457
irb(main):015:0> y = x.round_to(1)
=> 12.5
irb(main):016:0> y = x.round_to(-1)
=> 10.0

Note that 'rounding' the number is subject to floating point precision
errors. Subsequent printing of a number rounded to 3 decimal places
like the above may display as something like 12.4569999999999999999
 
Reply With Quote
 
 
 
 
Surjit Nameirakpam
Guest
Posts: n/a
 
      11-20-2007
I am getting the truncate but the round off mentioned above doesn't work
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-20-2007
On Nov 20, 1:31 pm, Surjit Nameirakpam <(E-Mail Removed)>
wrote:
> I am getting the truncate but the round off mentioned above doesn't work


As shown above, it certainly does work. (You are looking at the actual
output of typing code directly into irb and having Ruby evaluate it.)

I assume your code is slightly different. Perhaps if pared it down to
a test case that showed exactly your problem, we could help further.
 
Reply With Quote
 
Surjit Nameirakpam
Guest
Posts: n/a
 
      11-20-2007
Surjit Nameirakpam wrote:
> I am getting the truncate but the round off mentioned above doesn't work


Sorry.. Its the opposite round of works but truncate doesn't work
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bil Kleb
Guest
Posts: n/a
 
      11-20-2007
Surjit Nameirakpam wrote:
> I am getting the truncate but the round off mentioned above doesn't work


Please share your test, e.g., http://tinyurl.com/24825g

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov
 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      11-20-2007
Surjit Nameirakpam wrote:
> Surjit Nameirakpam wrote:
>> I am getting the truncate but the round off mentioned above doesn't work

>
> Sorry.. Its the opposite round of works but truncate doesn't work


Ah, I missed that you also wanted truncate.

irb(main):001:0> class Numeric
irb(main):002:1> # same potential floating point problem as round_to
irb(main):003:1* def truncate_to( decimals=0 )
irb(main):004:2> factor = 10.0**decimals
irb(main):005:2> (self*factor).floor / factor
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0>
irb(main):009:0* x = 12.45678
=> 12.45678
irb(main):010:0> x.truncate_to( 3 )
=> 12.456
irb(main):011:0> x.to_s.sub /(\.\d{3}).+/, '\1'
=> "12.456"
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to truncate/round-off decimal numbers? Girish Sahani Python 15 12-17-2011 01:06 AM
So, round and round... 70-682... MSLearning, please read! DaGenester MCITP 2 05-20-2010 04:36 PM
VS2005 - Going round and round in circles again :) postings@alexshirley.com ASP .Net 0 06-22-2006 11:26 AM
Computer Goes Round and Round Checking Alan Computer Support 6 10-08-2004 08:24 AM



Advertisments