简体   繁体   中英

How to only print decimals using printf if the number of decimals is greater than 0?

How would I go about using the printf() function to print a floating point number in such a way that I only print the decimal part if it is not 0? Examples:

1.0 -> 1
2.0 -> 2
1.5 -> 1.5
2.25 -> 2.25

etc.

printf("%g\n",5.0);
printf("%g\n",5.1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM