Forum: Ruby on Rails Formatting a float with a set number of decimals

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
chris hulbert (Guest)
on 2006-01-17 07:30
Another newbie question:

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

Thanks
chris hulbert (Guest)
on 2006-01-17 07:38
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
Bruce B. (Guest)
on 2006-01-17 07:47
(Received via mailing list)
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
Kevin O. (Guest)
on 2006-01-17 07:53
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
Dave S. (Guest)
on 2006-01-17 07:53
(Received via mailing list)
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/
chris hulbert (Guest)
on 2006-01-18 01:09
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 + "&nbsp;"
			else
				"(%.#{dec_places}f)" % (-v)
			end
		else
			'x&nbsp;'
		end
	end
Kevin O. (Guest)
on 2006-01-18 03:19
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 + "&nbsp;"
> 			else
> 				"(%.#{dec_places}f)" % (-v)
> 			end
> 		else
> 			'x&nbsp;'
> 		end
> 	end

Lots of problems here..

1. will break if you divide by zero
2. Should check to see if number is a subclass of Integer instead of not
a Float
3. if number is an Integer, when you divide by divide_by, you will
truncate the the value instead of rounding.

_Kevin
Nick S. (Guest)
on 2006-01-18 03:58
(Received via mailing list)
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? :)
Kevin O. (Guest)
on 2006-01-18 04:34
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
Nick S. (Guest)
on 2006-01-18 04:52
(Received via mailing list)
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*
This topic is locked and can not be replied to.