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?

30th Nov 2022, 3:18 AM
Ateeq Aslam
Ateeq Aslam - avatar
7 Respostas
+ 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
30th Nov 2022, 10:51 AM
Jayakrishna 🇮🇳
+ 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
30th Nov 2022, 3:26 AM
Giannis
Giannis - avatar
+ 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..
30th Nov 2022, 8:37 AM
Jayakrishna 🇮🇳
+ 2
Here is an example code including printing the number without its first digit. https://code.sololearn.com/cYn24TfS89kZ/?ref=app
30th Nov 2022, 2:41 PM
Lochard
Lochard - avatar
+ 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.
30th Nov 2022, 2:22 PM
Lochard
Lochard - avatar
0
Still confused..
30th Nov 2022, 10:38 AM
Ateeq Aslam
Ateeq Aslam - avatar
- 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:)
30th Nov 2022, 4:38 AM
Ateeq Aslam
Ateeq Aslam - avatar