Another newbie question: How do i convert a float to a string, rounded to a certain number of decimals? Thanks

on 2006-01-17 06:30

on 2006-01-17 06: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

on 2006-01-17 06:47

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 2006-01-17 06: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

on 2006-01-17 06:53

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/

on 2006-01-18 00: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 + " " else "(%.#{dec_places}f)" % (-v) end else 'x ' end end

on 2006-01-18 02: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 + " " > else > "(%.#{dec_places}f)" % (-v) > end > else > 'x ' > 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

on 2006-01-18 02:58

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 2006-01-18 03:34

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

on 2006-01-18 03:52

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*