How to specify the number of decimal places displayed in Ruby on Rails
The sprintf Ruby class provides the ability to specify the number of decimal places to display.
sprintf("%.1f",track.ratings.average('rating'))
In this example I'm getting the average ratings of a track and ensuring that the average is rounded to 1 decimal place.
About
Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.
Contact
my name at gmail.com
More snippets
Take a look in the archive
Need a website?
Contact my employer. Make sure to check out our portfolio of work.
Hosting
I recommend hostingrails.com
4 comments made
Thanks, I did a quick google, your entry popped up, its exactly what I needed :)
Thank you!
Nice snippet. Cheers!
Thanks paul, perfect!
Got something to say?