0

Sometimes I need double to be printed with 1 decimal (when it has the same value as integer), sometimes I need double to be printed with 6 decimals.

For example:

0.000 should be printed as 0.0    
1.000 should be printed as 1.0
1.200 should be printed as 1.200000
1.123456789 should be printed as 1.234568

How can I do this?

Commentary on possible duplicate

I am not sure my question is a duplicate of the other post. I am interested in a VARIABLE number of decimals. That is not touched upon there, as far as I can tell.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sasa Milenkovic
  • 299
  • 3
  • 5
  • To check if the number is an integer: https://stackoverflow.com/q/1521607/3052438 and then you can set precision the way the duplicate link shows. – Piotr Siupa May 14 '22 at 10:17
  • It is OK to disagree with a duplicate closure, Sasa, but it does matter how you do it. The blast you gave in the edit was so ferocious that I wonder if someone would ignore it even if your observations were correct. As with any human interactions, humility and amenability goes a long way - even if you are sure you are right. – halfer May 21 '22 at 13:01

0 Answers0