AlisdairO / pgexercises

PostgreSQL Exercises web site code
Other
400 stars 62 forks source link

Aggregates / Fachours3 - Rounding #24

Closed andyw8 closed 8 years ago

andyw8 commented 8 years ago

I'm curious why the solution uses:

trim(to_char(sum(bks.slots)/2.0, '9999999999999999D99'))

rather than the simpler:

round(sum(bks.slots) / 2.0, 2)

Am I missing some downside?

AlisdairO commented 8 years ago

to_char is the generally recommended solution, as it's reliable in all situations: http://stackoverflow.com/questions/13113096/how-to-round-an-average-to-2-decimal-places-in-postgresql . Thanks a lot for taking a look and raising the issue!