There is a code to always add .0, if needed, so # is not necessary.
And it has a side-effect of leaving too many trailing 0s.
I think the format for write/ln should be changed to %1.6g with the same trick of adding .0 whenever necessary.
write_canonical should stay as is: not clear how to improve it.