Floating point printing is tricky, as I am sure you know. You might want to refrefresh your understanding by consulting the literture--I know I would. For example, you might want to look at
Guy Steele's paper:
Guy L. Steele , Jon L. White, How to print floating-point numbers accurately, ACM SIGPLAN Notices, v.39 n.4, April 2004
is a classic and worthy of a read.