Velocity Reviews > Ruby > Rounding to X digits

# Rounding to X digits

Eric Anderson
Guest
Posts: n/a

 10-14-2004
This seems like such a basic question yet I can't really find the answer
anywhere in the docs (I'm probably looking in the wrong place).

Anyway I need to compute a percentage and output it. The percentage
should be in the form 39.45% (i.e. round to the nearest two decimal
places). So I have

top = 68
bottom = 271

percentage = (top.to_f / bottom * 100 * 100).round.to_f / 100

The above works but it does not seem very intuitive. What I would rather is:

percentage = (top.to_f / bottom).round(4) * 100

But round does not take any arguments on how many decimal places I want.
It just assumes I don't want any. Perhaps there is another function that
I am not seeing that will round while keeping a certain number of
decimal places. Obviously I could also enhance round to take an optional
argument but I wanted to see if there was an already existing function
in the Ruby std library that will do it for me.

Thanks,

Eric

Eric Anderson
Guest
Posts: n/a

 10-14-2004
Eric Anderson wrote:
> Obviously I could also enhance round to take an optional
> argument but I wanted to see if there was an already existing function
> in the Ruby std library that will do it for me.

To follow up my own post. If there isn't a function like what I am
looking for in the standard library, I am using the following to make it
like I want.

class Float
alias ldround :round
def round( sd=0 )
return (self * (10 ** sd)).oldround.to_f / (10**sd)
end
end

James Edward Gray II
Guest
Posts: n/a

 10-14-2004
On Oct 14, 2004, at 8:19 AM, Eric Anderson wrote:

> This seems like such a basic question yet I can't really find the
> answer anywhere in the docs (I'm probably looking in the wrong place).
>
> Anyway I need to compute a percentage and output it. The percentage
> should be in the form 39.45% (i.e. round to the nearest two decimal
> places).

You're looking for sprintf():

sprintf "%.2f", 39.456789 # => 39.46

The ri docs for sprintf() will explain the format string options.

It also has a cousin, printf() that can used as the data is sent to
some IO object.

Finally, the String class allows a shortcut:

"%.2f" % 39.456789 # => 39.46

Hope that helps.

James Edward Gray II

Eric Anderson
Guest
Posts: n/a

 10-14-2004
James Edward Gray II wrote:
> You're looking for sprintf():

I didn't even think about sprintf. I guess I was so fixed on looking for
a way to round numbers in the various Numeric objects that it didn't
occur to me to look in the string functionality.

sprintf() still seems a bit to C-ish for me. I kind of like my round
extension that allows for an optional arg. The question is my
implementation more efficient or using sprintf more efficient? Benchmark
to the rescue:

My implementation through 1 million iterations:

user system total real
No Decimal: 5.410000 0.000000 5.410000 ( 5.413982)
5 Decimal Places: 13.260000 0.000000 13.260000 ( 13.259163)
13 Decimal Places: 21.490000 0.000000 21.490000 ( 21.481821)

Sprintf implementation through 1 million iterations:

user system total real
No Decimal: 12.400000 0.000000 12.400000 ( 12.362547)
5 Decimal Places: 15.310000 0.000000 15.310000 ( 15.549580)
13 Decimal Places: 14.700000 0.020000 14.720000 ( 16.145235)

Looks like my implementation leads for the case where there are less
decimal places remaining (the more common case) and sprintf leads when
there are more. Of course at this point we are really just splitting
hairs.

For reference here are the implementations and benchmark script:

# My implementation
class Float
alias ldround :round
def round( sd=0 )
return (self * (10 ** sd)).oldround.to_f / (10**sd)
end
end

# Sprintf implementation
class Float
alias ldround :round
def round( sd=0 )
return ("%.#{sd}f" % self).to_f
end
end

Benchmark script:

#!/usr/bin/ruby

require 'benchmark'
include Benchmark

require 'sprintf_round'

n = 1000000
pi = 3.14159265358979
bm(17) do |test|
test.report('No Decimal:') do
n.times do |x|
pi.round
end
end
test.report('5 Decimal Places:') do
n.times do |x|
pi.round(5)
end
end
test.report('13 Decimal Places:') do
n.times do |x|
pi.round(13)
end
end
end

Robert Klemme
Guest
Posts: n/a

 10-14-2004

"Eric Anderson" <(E-Mail Removed)> schrieb im Newsbeitrag
news:GZvbd.111\$(E-Mail Removed)...
> James Edward Gray II wrote:
> > You're looking for sprintf():

>

*Hands over some cooling ice*

> I didn't even think about sprintf. I guess I was so fixed on looking for
> a way to round numbers in the various Numeric objects that it didn't
> occur to me to look in the string functionality.
>
> sprintf() still seems a bit to C-ish for me. I kind of like my round
> extension that allows for an optional arg. The question is my
> implementation more efficient or using sprintf more efficient? Benchmark
> to the rescue:

The difference in efficiency is one issue. But there is a conceptual
difference: it depends whether you want to align printed output or round a
numeric value in a calculation. For the first task sprintf is probably
better because the amount of places printed is guaranteed (by the format
string), while it's not if you just round the number and print it.

Kind regards

robert

Markus
Guest
Posts: n/a

 10-14-2004
Here's mine (with a few extras):

class Numeric
def iff(cond)
cond ? self : self-self
end
def to_the_nearest(n)
n*(self/n).round
end
def aprox(x,n=0.001)
(to_the_nearest n) == (x.to_the_nearest n)
end
def at_least(x); (self >= x) ? self : x; end
def at_most(x); (self <= x) ? self : x; end
end

-- Markus

On Thu, 2004-10-14 at 06:19, Eric Anderson wrote:
> This seems like such a basic question yet I can't really find the answer
> anywhere in the docs (I'm probably looking in the wrong place).
>
> Anyway I need to compute a percentage and output it. The percentage
> should be in the form 39.45% (i.e. round to the nearest two decimal
> places). So I have
>
> top = 68
> bottom = 271
>
> percentage = (top.to_f / bottom * 100 * 100).round.to_f / 100
>
> The above works but it does not seem very intuitive. What I would rather is:
>
> percentage = (top.to_f / bottom).round(4) * 100
>
> But round does not take any arguments on how many decimal places I want.
> It just assumes I don't want any. Perhaps there is another function that
> I am not seeing that will round while keeping a certain number of
> decimal places. Obviously I could also enhance round to take an optional
> argument but I wanted to see if there was an already existing function
> in the Ruby std library that will do it for me.
>
> Thanks,
>
> Eric
>

trans. (T. Onoma)
Guest
Posts: n/a

 10-15-2004
On Thursday 14 October 2004 01:55 pm, Markus wrote:
| Here's mine (with a few extras):
|
| class Numeric
| def iff(cond)
| cond ? self : self-self
| end
| def to_the_nearest(n)
| n*(self/n).round
| end
| def aprox(x,n=0.001)
| (to_the_nearest n) == (x.to_the_nearest n)
| end
| def at_least(x); (self >= x) ? self : x; end
| def at_most(x); (self <= x) ? self : x; end
| end
|

| def to_the_nearest(n)
| n*(self/n).round
| end
def round_to_nearest( n=0.01 )
Â*Â* (self * (1/n)).round.to_f / (1/n)
Â*Â*end

For some reason they don't give the exact same answer. Try an edge case.

irb(main):066:0> 0.1 * (134.45/0.1).round
=> 134.4
irb(main):067:0> (134.45 * (1/0.1)).round.to_f / (1/0.1)
=> 134.5

T.

Markus
Guest
Posts: n/a

 10-15-2004
Interesting. I hope to heck you used a script to find that edge
case. What made you suspect there would be a difference, given that
they are mathematically equivalent?

I'll definitely come back to this after the coffee hits.

-- Markus

On Fri, 2004-10-15 at 07:28, trans. (T. Onoma) wrote:
> On Thursday 14 October 2004 01:55 pm, Markus wrote:
> | Here's mine (with a few extras):
> |
> | class Numeric
> | def iff(cond)
> | cond ? self : self-self
> | end
> | def to_the_nearest(n)
> | n*(self/n).round
> | end
> | def aprox(x,n=0.001)
> | (to_the_nearest n) == (x.to_the_nearest n)
> | end
> | def at_least(x); (self >= x) ? self : x; end
> | def at_most(x); (self <= x) ? self : x; end
> | end
> |
>
> | def to_the_nearest(n)
> | n*(self/n).round
> | end
> def round_to_nearest( n=0.01 )
> (self * (1/n)).round.to_f / (1/n)
> end
>
> For some reason they don't give the exact same answer. Try an edge case.
>
> irb(main):066:0> 0.1 * (134.45/0.1).round
> => 134.4
> irb(main):067:0> (134.45 * (1/0.1)).round.to_f / (1/0.1)
> => 134.5
>
> T.

Ara.T.Howard@noaa.gov
Guest
Posts: n/a

 10-15-2004
On Fri, 15 Oct 2004, trans. (T. Onoma) wrote:

> On Thursday 14 October 2004 01:55 pm, Markus wrote:
> | Here's mine (with a few extras):
> |
> | class Numeric
> | def iff(cond)
> | cond ? self : self-self
> | end
> | def to_the_nearest(n)
> | n*(self/n).round
> | end
> | def aprox(x,n=0.001)
> | (to_the_nearest n) == (x.to_the_nearest n)
> | end
> | def at_least(x); (self >= x) ? self : x; end
> | def at_most(x); (self <= x) ? self : x; end
> | end
> |
>
> | def to_the_nearest(n)
> | n*(self/n).round
> | end
> def round_to_nearest( n=0.01 )
> Â*Â* (self * (1/n)).round.to_f / (1/n)
> Â*Â*end
>
> For some reason they don't give the exact same answer. Try an edge case.
>
> irb(main):066:0> 0.1 * (134.45/0.1).round
> => 134.4
> irb(main):067:0> (134.45 * (1/0.1)).round.to_f / (1/0.1)
> => 134.5

jib:~/eg/ruby > cat truncate.rb
class Float
def truncate sd = 2
i = self.to_i
prec = 10 ** sd
fraction = (i.zero? ? self : (self / i) - 1)
i + (fraction * prec).to_i / prec.to_f
end
end

p(0.1 * (134.45/0.1).truncate) #=> 134.4
p((134.45 * (1/0.1)).truncate.to_f / (1/0.1)) #=> 134.4

jib:~/eg/ruby > ruby truncate.rb
134.4
134.4

regards.

-a
--
================================================== =============================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| When you do something, you should burn yourself completely, like a good
| bonfire, leaving no trace of yourself. --Shunryu Suzuki
================================================== =============================

Markus
Guest
Posts: n/a

 10-15-2004
On Fri, 2004-10-15 at 07:28, trans. (T. Onoma) wrote:
> On Thursday 14 October 2004 01:55 pm, Markus wrote:
> | Here's mine (with a few extras):
> |
> | class Numeric
> | def to_the_nearest(n)
> | n*(self/n).round
> | end
> | end
> |
>
> def round_to_nearest( n=0.01 )
> (self * (1/n)).round.to_f / (1/n)
> end
>
> For some reason they don't give the exact same answer. Try an edge case.
>
> irb(main):066:0> 0.1 * (134.45/0.1).round
> => 134.4
> irb(main):067:0> (134.45 * (1/0.1)).round.to_f / (1/0.1)
> => 134.5

Peeling one layer of the onion, they differ because:

irb(main):033:0> printf "%20.15f",(134.45 * (1.0/0.1))
1344.500000000000000=> nil
irb(main):034:0> printf "%20.15f",(134.45/0.1)
1344.499999999999773=> nil

I suppose this is not unexpected (my mama warned me 'bout floats) but it
is a little unexpected--no, I'm wrong, 1/5 is a repeating decimal base
2, so it's perfectly expected. The tricky bit is, which form (if either
of them) will always (or at least, more generally) give the correct
result? I can't see off hand that either will be intrinsically "better"
but I could be missing a point. I suspect (SWAG) that the hybrid:

def to_the_nearest(n)
if self.abs < 1.0
(self/n).round/n
else
m = 1.0/m
(self*m).round/m
end
end

would do better than both, but I haven't tested it.

-- Markus

P.S. Mine has mostly been tested on values < 1.0, thus my suspicion that
it works well in that domain. Typing this though, I realize that it was
tested FOR CONFORMANCE WITH A PRE-EXISTING SYSTEM* which itself may have
been buggy.

* Think 25 year old spaghetti FORTRAN, then sigh in bliss realizing that
your imagination is much nicer than the ugly facts.