Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Example: Create a printf statement that uses float precision to print the following output: 3.21.

What I have tried:

How to use float precision to print digits before decimal?
Posted
Updated 3-Oct-22 6:32am
v3

'Left pad' with '0':
printf("%07.3f", 3.3);
results in '003.300'


'Left pad' with space:
printf("%7.3f", 3.3);
results in ' 3.300'

Simply googled, try i also ;)
 
Share this answer
 
Comments
CPallini 3-Oct-22 16:05pm    
5.
0x01AA 4-Oct-22 3:49am    
Thank you.
 
Share this answer
 
Comments
CPallini 3-Oct-22 16:05pm    
5.
Richard MacCutchan 3-Oct-22 16:14pm    
Thanks, easy to find with Google.
Take a look at the printf documentation printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com[^]
In particular pay attention to the width and precision specifiers. In particular, the width determines the minimum width of the printed field, and precision specifies the number of places after the decimal to display. Note that the width includes the decimal point and the leading sign.
 
Share this answer
 
Comments
0x01AA 3-Oct-22 13:18pm    
"Note that the width includes the decimal point and the leading sign."
--> +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900