chris hulbert
2006-Jan-17 04:54 UTC
[Rails] Formatting a float with a set number of decimals
Another newbie question: How do i convert a float to a string, rounded to a certain number of decimals? Thanks -- Posted via http://www.ruby-forum.com/.
chris hulbert
2006-Jan-17 05:02 UTC
[Rails] Re: Formatting a float with a set number of decimals
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
--
Posted via http://www.ruby-forum.com/.
Bruce Balmer
2006-Jan-17 05:09 UTC
[Rails] Re: Formatting a float with a set number of decimals
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 On 16-Jan-06, at 10:38 PM, chris hulbert wrote:> 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Dave Silvester
2006-Jan-17 05:15 UTC
[Rails] Re: Formatting a float with a set number of decimals
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 Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Kevin Olbrich
2006-Jan-17 05:17 UTC
[Rails] Re: Formatting a float with a set number of decimals
chris hulbert wrote:> Another newbie question: > > How do i convert a float to a string, rounded to a certain number of > decimals? > > ThanksThis works... "%5.2f" % number -- Posted via http://www.ruby-forum.com/.
chris hulbert
2006-Jan-17 22:33 UTC
[Rails] Re: Re: Formatting a float with a set number of decimals
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
--
Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jan-18 00:43 UTC
[Rails] Re: Re: Formatting a float with a set number of decimals
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 > endLots 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 -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-18 01:20 UTC
[Rails] Re: Re: Formatting a float with a set number of decimals
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? :)
On 1/17/06, chris hulbert <chris.hulbert@ruralpress.com>
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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Kevin Olbrich
2006-Jan-18 01:58 UTC
[Rails] Re: Re: Re: Formatting a float with a set number of decimals
Nick Stuart 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 -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-18 02:14 UTC
[Rails] Re: Re: Re: Formatting a float with a set number of decimals
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* On 1/17/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:> Nick Stuart 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >