0
How to %f to output the desired digit of decimal & floats(aka after point)
If I want this code to output 89.123 #include <stdio.h> int main() { printf("float is: %f",589.1234); return 0; } How that can be possible?? .2%f => understood How to output desired decimal numbers?
7 Réponses
+ 2
About what?
Before dot is the maximum width to set in output screen for complete value.
( if width is above specified width then it has no effect. if width is less then it is adjusted to maximum width to be used for the value on the output screen. )
After decimal point, it specifies the number of decimal places to round up the value
+ 2
printf("%x.yf", number);
where x is the amount of digits you want to print before the point and y the amount of digits after the point
+ 2
"2.2f" => here before point 2 is the *maximum* length formatted to, for the float value. Output : 345.35 has length 6 including point. This does not cut digits, instead if digits is less then adds a space or fills with included formater like '0' before. . 005.75
After decimal point 2 will round up decimal part to 2 decimal points.. So .345 will be rounded to 2decimal places so results as .35
Hope it helps..
+ 2
Here is an example code including printing the number without its first digit.
https://code.sololearn.com/cYn24TfS89kZ/?ref=app
+ 1
In `printf("%x.yf", someFloat);` the x represent the total digits to be printed, it will self-adjust to the length of the integer part if it is shorter or left out. y is the digits after decimal point to be printed.
0
Still confused..
- 1
Then:
include <stdio.h>
int main() {
printf("%2.2f",345.345);
return 0;
}
Output: 345.35
Why not printing two digit before point?
Using Sololearn editor:)