Take the 2-minute tour ×

I am displaying a large number of vehicle records (120k+), each record has an engine size for that vehicle. The engine size can either in one of two formats:

"1995" #cc's

If the engine size is saved as a 4 char string I want to abbreviate in the view, to the nearest 100th - for example "1995" should get displayed as "2.0" and "1900" should get displayed as "1.9".

What is the best way I can do this? (cannot update database - this is view logic only)


share|improve this question
Do you want the nearest, or do you want it rounded down (i.e., truncated)? Also, you meant 10th, not 100th. –  David M. Sep 20 '10 at 23:22
add comment

3 Answers

up vote 2 down vote accepted
(size.to_f / 100).round / 10.0
share|improve this answer
add comment

I don't know what other cases you have, but if you can separate the two cases by a simple comparison, it is

if size > 1000 # or whatever condition here
  sprintf("%.1f", size.to_f / 1000)
  sprintf("%.1f", size)
share|improve this answer
add comment

Try this:

size = (size.length == 4) ? (size.to_f / 100).round / 10.0 : size.to_f
share|improve this answer
add comment

Your Answer


