I'm not sure you can with a custom format if you also want to show 1, 3, 4, or 5 decimals for other numbers. With custom formats, you are specifying the exact format to use and it will format the number as you specified. Your format will display up to 5 decimals but will drop trailing zeros.
You can make your format "#,###.00000" which will always show all five decimal places. Click on the disclosure triangle next to ".#####" and choose "show trailing zeros". The number 1.40 will display as 1.40000. If you want it to be 1.40 you need to reduce the number of decimal places in your format to two, but then all numbers you enter will be 2 decimals.
The only format that will adjust to what you type is "automatic". If you type 2 decimals, it will format the cell for 2 decimals. Type 3 decimals and is sets it to 3 decimals. But, as you already know, it has no way to make a zero invisible.
An alternative to your format would be to use a conditional highlighting rule to set the text opacity to zero when the number is zero. It will make it invisible.
- Select the cells that should get the highlighting rule
- Set the cell format to automatic
- Click on "conditional highlight"
- Click to add a rule
- Choose "equal to"
- Type a zero in the box
- For the format, choose "custom style" (scroll down to the end to see that one)
- Click on the color wheel next to the text color
- On the "wheel" settings, set the opacity to zero.
- Back in the format sidebar click "done"
If the information in the cells is data (not formulas), you can copy/paste the format to other cells by copy/pasting one of the formatted cells. If the cell to receive the format has a formula, you'll have to use Copy Style and Paste Style.