tags:

views:

53

answers:

3

Hi,

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:

"1.8"
OR
"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)

Thanks!

+2  A: 
(size.to_f / 100).round / 10.0
Matthew Flaschen
A: 
size = (size.length == 4) ? (size.to_f / 100).round / 10.0 : size.to_f
You
A: 

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)
else
  sprintf("%.1f", size)
end
Chubas