Skip to content

How to specify the number of decimal places displayed in Ruby on Rails

Posted on 14 August 2006

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.

View the snippets archive ››

4 comments made

Christopher Mar commented on 03 Feb 08 at 06:48

Thanks, I did a quick google, your entry popped up, its exactly what I needed :)

Peter commented on 15 Mar 08 at 09:23

Thank you!

Stephan commented on 22 May 08 at 10:17

Nice snippet. Cheers!

Rob Aldred - Digital Marketing commented on 04 Jul 08 at 04:34

Thanks paul, perfect!

Got something to say?

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