Another newbie question:

How do i convert a float to a string, rounded to a certain number of

decimals?

Thanks

Another newbie question:

How do i convert a float to a string, rounded to a certain number of

decimals?

Thanks

Is this it?

sprintf("%0.2f",10.12345)

Is there a better way?

chris hulbert wrote:

Another newbie question:

How do i convert a float to a string, rounded to a certain number of

decimals?Thanks

Chris:

I don’t KNOW the answers to your question (which may lead you to

wonder why I wrote). Depending on the situation, I’d suggest you look

up one of these two.

There is a helper called number_with_precision. It works like this,

if x is the number

number_with_precision(x, number of decimal places)

To convert a number to a string, assuming the number was x

x.to_s

Hope that helps. People on this list are always helping me.

bruce

chris hulbert wrote:

Another newbie question:

How do i convert a float to a string, rounded to a certain number of

decimals?Thanks

This works…

“%5.2f” % number

On Tuesday 17 Jan 2006 05:38, chris hulbert wrote:

Is this it?

sprintf("%0.2f",10.12345)

Is there a better way?

Assuming you’re dealing with currency, in my case it’s GBP (so you can

replace

the £ with $ or Euro whatever you need), but here’s what I have in

my

application_helper.rb

def display_currency(number)

unless number.nil?

“£” + sprintf("%01.2f",number)

end

end

Then in my views I can just do:

<%= display_currency @price %>

~Dave

–

Dave S.

Rent-A-Monkey Website Development

Web: http://www.rentamonkey.com/

Thanks for all the input, i ended up with this, and indeed it is for

currency:

```
def n(number,dec_places=0,divide_by=-1)
# given a number, rounds it, divides it by Y and stringifies it
# or if it is infinite, returns an 'x'
if !number.nil? && (!(number.kind_of? Float) || number.finite?)
v = number
v /= divide_by if divide_by != -1
if v >= 0 then
"%.#{dec_places}f" % v + " "
else
"(%.#{dec_places}f)" % (-v)
end
else
'x '
end
end
```

Chris, I had a similar requirement (be able to round to arbitrary

amount of decimals). What I did was made a very simple plugin with one

class in it:

class Float

alias original_round round

def round(precision = 0)

if precision == 0

original_round

else

sprintf("%.#{precision}f", self).to_f

end

end

end

This overrides the Float class so now you can just do:

23.43.round #=23

23.43.round(1) #= 23.4

etc…

It works like a champ. Dont know if there would be any issues with it

in the long run, but couldn’t think of any.

-Nick

p.s. just thought, I should probably and a check for negative

percision, but why would anyone want to even try that?

Nick S. wrote:

It works like a champ. Dont know if there would be any issues with it

in the long run, but couldn’t think of any.-Nick

p.s. just thought, I should probably and a check for negative

percision, but why would anyone want to even try that?

You should check for a decimal precision too…

_Kevin

Hrmmm, that would be good too. Of course right now I’m the only

developer in my company, and I know better.

*famous last words*

chris hulbert wrote:

Thanks for all the input, i ended up with this, and indeed it is for

currency:def n(number,dec_places=0,divide_by=-1)

# given a number, rounds it, divides it by Y and stringifies it

# or if it is infinite, returns an ‘x’

if !number.nil? && (!(number.kind_of? Float) || number.finite?)

v = number

v /= divide_by if divide_by != -1

if v >= 0 then

“%.#{dec_places}f” % v + " "

else

“(%.#{dec_places}f)” % (-v)

end

else

'x ’

end

end

Lots of problems here…

- will break if you divide by zero
- Should check to see if number is a subclass of Integer instead of not

a Float - if number is an Integer, when you divide by divide_by, you will

truncate the the value instead of rounding.

_Kevin

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

Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs