I hold decimals in a database using DECIMAL(10,5)
I would like to format these numbers according to a few rules:
- A zero decimal should display as 0
- Show a long decimal (no trailing zero’s) with all of it’s numbers
- When possible, I would like to only show up to 2 decimal places (when there are trailing zeros)
Here are some examples:
- The left side corresponds to how the number is stored in database.
- The right number is how I would like to display the number in my application.
0.00000 => 0
0.51231 => 0.51231
0.12000 => 0.12
0.40000 => 0.40
0.67800 => 0.678
12.10000 => 12.10
This will work for you:
Example